perm filename BOOK1.XGP[LSP,JRA] blob sn#242100 filedate 1976-10-19 generic text, type T, neo UTF8
/LMAR=0/XLINE=3/FONT#0=BASL30/FONT#1=BASB30/FONT#2=ASI30[LSP,JRA]/FONT#3=SUB/FONT#4=ASI30[LSP,JRA]/FONT#5=BASB30/FONT#6=TREE[LSP,JRA]/FONT#7=SUP/FONT#8=SPEC[LSP,JRA]/FONT#9=SET1/FONT#10=GRFX25[LSP,JRA]/FONT#11=FIX30/FONT#12=NGB30/FONT#13=GERM35/FONT#14=MG[LSP,JRA]/FONT#15=GRFX35
␈↓ ↓H␈↓␈↓↓␈↓ 	sCONTENTS     i␈↓


␈↓ ↓H␈↓↓␈↓ ∧ZT A B L E   O F   C O N T E N T S



␈↓ ↓H␈↓↓CHAPTER␈↓ ¬PAGE␈↓


␈↓ ↓H␈↓␈↓ ↓x1␈↓ α8THE ANATOMY OF LISP␈↓ I1


␈↓ ↓H␈↓␈↓ ↓x2␈↓ α8SYMBOLIC EXPRESSIONS␈↓ I6

␈↓ ↓H␈↓␈↓ βλ2.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ? 6
␈↓ ↓H␈↓␈↓ βλ2.2␈↓ βhSymbolic Expressions: abstract data structures␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ? 9
␈↓ ↓H␈↓␈↓ βλ2.3␈↓ βhTrees: representations of Symbolic expressions␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 12
␈↓ ↓H␈↓␈↓ βλ2.4␈↓ βhPrimitive Functions␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 15
␈↓ ↓H␈↓␈↓ βλ2.5␈↓ βhPredicates and Conditional Expressions␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 21
␈↓ ↓H␈↓␈↓ βλ2.6␈↓ βhSequences: abstract data structures␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 29
␈↓ ↓H␈↓␈↓ βλ2.7␈↓ βhLists: representations of sequences␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 34
␈↓ ↓H␈↓␈↓ βλ2.8␈↓ βhA respite␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 41
␈↓ ↓H␈↓␈↓ βλ2.9␈↓ βhBecoming an expert␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 44


␈↓ ↓H␈↓␈↓ ↓x3␈↓ α8APPLICATIONS OF LISP␈↓ :53

␈↓ ↓H␈↓␈↓ βλ3.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 53
␈↓ ↓H␈↓␈↓ βλ3.2␈↓ βhExamples of LISP applications␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 55
␈↓ ↓H␈↓␈↓ βλ3.3␈↓ βhDifferentiation␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 56
␈↓ ↓H␈↓␈↓ βλ3.4␈↓ βhData Bases␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 65
␈↓ ↓H␈↓␈↓ βλ3.5␈↓ βhAlgebra of polynomials␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 67
␈↓ ↓H␈↓␈↓ βλ3.6␈↓ βhEvaluation of polynomials␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 71
␈↓ ↓H␈↓␈↓ βλ3.7␈↓ βhThe great mothers␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 81
␈↓ ↓H␈↓␈↓ βλ3.8␈↓ βhAnother Respite␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 82
␈↓ ↓H␈↓␈↓ βλ3.9␈↓ βhProving properties of programs␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 84


␈↓ ↓H␈↓␈↓ ↓x4␈↓ α8EVALUATION OF LISP EXPRESSIONS␈↓ :86

␈↓ ↓H␈↓␈↓ βλ4.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 86
␈↓ ↓H␈↓␈↓ βλ4.2␈↓ βhS-expr translation of LISP expressions␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 91
␈↓ ↓H␈↓␈↓ βλ4.3␈↓ βhSymbol tables␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 93
␈↓ ↓H␈↓␈↓ βλ4.4␈↓ βh␈↓αλ␈↓-notation␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 95
␈↓ ↓H␈↓␈↓ βλ4.5␈↓ βhMechanization of evaluation␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ 0 99
␈↓ ↓H␈↓␈↓ βλ4.6␈↓ βhExamples of ␈↓αeval␈↓␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 102
␈↓ ↓H␈↓␈↓ βλ4.7␈↓ βhVariables␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 110
␈↓ ↓H␈↓␈↓ βλ4.8␈↓ βhEnvironments and bindings␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 113
␈↓ ↓H␈↓␈↓ βλ4.9␈↓ βh␈↓αlabel␈↓␈↓ ∧8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 118
␈↓ ↓H␈↓␈↓ βλ4.10␈↓ βhFunctional Arguments and Functional Values␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 120
␈↓ ↓H␈↓␈↓↓ii  CONTENTS␈↓ X␈↓


␈↓ ↓H␈↓␈↓ βλ4.11␈↓ βhBinding strategies and implementations␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 131
␈↓ ↓H␈↓␈↓ βλ4.12␈↓ βhSpecial Forms␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 134
␈↓ ↓H␈↓␈↓ βλ4.13␈↓ βhThe ␈↓αprog␈↓-feature␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 136
␈↓ ↓H␈↓␈↓ βλ4.14␈↓ βhAlternatives to ␈↓αprog␈↓␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 143
␈↓ ↓H␈↓␈↓ βλ4.15␈↓ βhExtensions to ␈↓αeval␈↓␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 146
␈↓ ↓H␈↓␈↓ βλ4.16␈↓ βhNon-recursive control structures␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 147
␈↓ ↓H␈↓␈↓ βλ4.17␈↓ βh␈↓αeval␈↓ with explicit access␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 148
␈↓ ↓H␈↓␈↓ βλ4.18␈↓ βh␈↓αeval␈↓ with explicit control␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 155
␈↓ ↓H␈↓␈↓ βλ4.19␈↓ βhAn evaluator for ␈↓αprog␈↓␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 159
␈↓ ↓H␈↓␈↓ βλ4.20␈↓ βhAlternatives to ␈↓αeval␈↓␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 168
␈↓ ↓H␈↓␈↓ βλ4.21␈↓ βhFunction definitions␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 171
␈↓ ↓H␈↓␈↓ βλ4.22␈↓ βhRapprochement: In retrospect␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 174
␈↓ ↓H␈↓␈↓ βλ4.23␈↓ βhThe LISP machine␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 177


␈↓ ↓H␈↓␈↓ ↓x5␈↓ α8IMPLEMENTATION OF THE STATIC STRUCTURE OF LISP␈↓ +181

␈↓ ↓H␈↓␈↓ βλ5.1␈↓ βhIntroduction␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 181
␈↓ ↓H␈↓␈↓ βλ5.2␈↓ βhRepresentation of S-expressions␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 182
␈↓ ↓H␈↓␈↓ βλ5.3␈↓ βhRepresentation of LISP primitives␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 185
␈↓ ↓H␈↓␈↓ βλ5.4␈↓ βhA few programming techniques␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 191
␈↓ ↓H␈↓␈↓ βλ5.5␈↓ βhSymbol tables and property-lists␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 192
␈↓ ↓H␈↓␈↓ βλ5.6␈↓ βhProperty-list functions␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 197
␈↓ ↓H␈↓␈↓ βλ5.7␈↓ βhAn ␈↓αeval␈↓ for p-lists␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 198
␈↓ ↓H␈↓␈↓ βλ5.8␈↓ βhRepresentation of property-lists␈↓ πλ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 201
␈↓ ↓H␈↓␈↓ βλ5.9␈↓ βhA picture of the atom ␈↓αNIL␈↓␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 206
␈↓ ↓H␈↓␈↓ βλ5.10␈↓ βhInput/Output: ␈↓αread␈↓ and ␈↓αprint␈↓␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 207
␈↓ ↓H␈↓␈↓ βλ5.11␈↓ βhTable searching: Hashing␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 211
␈↓ ↓H␈↓␈↓ βλ5.12␈↓ βhA first look at ␈↓αcons␈↓␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 216
␈↓ ↓H␈↓␈↓ βλ5.13␈↓ βhStorage management: garbage collection␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 217
␈↓ ↓H␈↓␈↓ βλ5.14␈↓ βhA review of the structure of  the LISP machine␈↓ λH .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 222
␈↓ ↓H␈↓␈↓ βλ5.15␈↓ βhImplementations of binding␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 224
␈↓ ↓H␈↓␈↓ βλ5.16␈↓ βhstack implementation of deep binding␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 226
␈↓ ↓H␈↓␈↓ βλ5.17␈↓ βhstack implementation of shallow binding␈↓ πh .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 227
␈↓ ↓H␈↓␈↓ βλ5.18␈↓ βhStrategies for LISP implementation␈↓ π8 .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 233
␈↓ ↓H␈↓␈↓ βλ5.19␈↓ βhEpilogue␈↓ ∧h .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 234


␈↓ ↓H␈↓␈↓ ↓x6␈↓ α8PROJECTS␈↓ +236

␈↓ ↓H␈↓␈↓ βλ6.1␈↓ βhExtensions to ␈↓αeval␈↓␈↓ ¬H .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 236
␈↓ ↓H␈↓␈↓ βλ6.2␈↓ βhPretty-printing␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 238
␈↓ ↓H␈↓␈↓ βλ6.3␈↓ βhData Bases␈↓ ¬_ .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 239
␈↓ ↓H␈↓␈↓ βλ6.4␈↓ βhSyntax-directed processes␈↓ ε( .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 239
␈↓ ↓H␈↓␈↓ βλ6.5␈↓ βhSyntax-directed I/O␈↓ ¬x .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 244
␈↓ ↓H␈↓␈↓ βλ6.6␈↓ βhSyntax directed computation␈↓ εX .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  .  . ␈↓ ! 248
␈↓ ↓H␈↓␈↓↓␈↓ 	aCONTENTS     iii␈↓


␈↓ ↓H␈↓␈↓ ↓xBIBLIOGRAPHY␈↓ +250



␈↓ ↓H␈↓␈↓ ↓xINDEX␈↓ +259
␈↓ ↓H␈↓␈↓↓1.␈↓ 	mIntroduction     1␈↓


␈↓ ↓H␈↓␈↓ ε␈↓↓SECTION 1

␈↓ ↓H␈↓↓␈↓ ¬→THE ANATOMY OF LISP␈↓




␈↓ ↓H␈↓␈↓ ∧λ"...␈α∞it␈α∂is␈α∞important␈α∂not␈α∞to␈α∞lose␈α∂sight␈α∞of␈α∂the␈α∞fact␈α∞that␈α∂there␈α∞is␈α∂a␈α∞difference
␈↓ ↓H␈↓␈↓ ∧λbetween␈α⊃training␈α⊃and␈α⊃education.␈α∩ If␈α⊃computer␈α⊃science␈α⊃is␈α∩a␈α⊃fundamental
␈↓ ↓H␈↓␈↓ ∧λdiscipline,␈α_then␈α↔university␈α_education␈α_in␈α↔this␈α_field␈α_should␈α↔emphasize
␈↓ ↓H␈↓␈↓ ∧λenduring fundamental principles rather than transient current technology."

␈↓ ↓H␈↓␈↓ ε]Peter Wegner, ␈↓αThree Computer Cultures␈↓[Weg 70] 

␈↓ ↓H␈↓This␈α∞text␈α∞is␈α
nominally␈α∞about␈α∞LISP␈α∞and␈α
data␈α∞structures.␈α∞However␈α∞it␈α
in␈α∞fact␈α∞covers␈α∞much␈α
broader
␈↓ ↓H␈↓areas␈α
of␈αcomputer␈α
science.␈α The␈α
author␈αhas␈α
long␈αfelt␈α
that␈αthe␈α
beginning␈αstudent␈α
of␈αcomputer␈α
science
␈↓ ↓H␈↓has␈α∂been␈α∂getting␈α∂a␈α∂distorted␈α∂and␈α∂disjointed␈α∂picture␈α∂of␈α∂the␈α∂field.␈α∂In␈α∂some␈α∂ways␈α∂this␈α∂confusion␈α∞is
␈↓ ↓H␈↓natural;␈αthe␈αfield␈αhas␈αbeen␈αgrowing␈αat␈αsuch␈αan␈αrapid␈αrate␈αthat␈αfew␈αof␈αus␈αare␈αprepared␈αto␈αbe␈α
judged
␈↓ ↓H␈↓experts␈α
in␈α
all␈α
areas␈α
of␈α
the␈α
discipline.␈α
 The␈αcurrent␈α
alternative␈α
seems␈α
to␈α
be␈α
to␈α
give␈α
a␈αfew␈α
introductory
␈↓ ↓H␈↓courses␈α⊂in␈α⊂programming␈α⊂and␈α⊂machine␈α⊂organization␈α⊂followed␈α⊂by␈α⊂relatively␈α⊂specialized␈α⊂courses␈α∂in
␈↓ ↓H␈↓more␈α
technical␈α
areas.␈α
The␈αdifficulty␈α
with␈α
this␈α
approach␈α
is␈αthat␈α
much␈α
of␈α
the␈α
technical␈αmaterial␈α
never
␈↓ ↓H␈↓gets␈αrelated.␈αThe␈αstudent's␈αperspective␈αand␈αmotivation␈αsuffers␈αin␈αthe␈αprocess.␈αThis␈αbook␈αuses␈αLISP
␈↓ ↓H␈↓as␈αa␈αmeans␈αfor␈αrelating␈αtopics␈αwhich␈αnormally␈αget␈αtreated␈αin␈αseveral␈αseparate␈αcourses.␈αThe␈αpoint␈αis
␈↓ ↓H␈↓not␈α∞that␈α
we␈α∞␈↓↓can␈↓␈α
do␈α∞this␈α
in␈α∞LISP,␈α
but␈α∞rather␈α
that␈α∞it␈α
is␈α∞␈↓↓natural␈↓␈α
to␈α∞do␈α
it␈α∞in␈α
LISP.␈α∞ The␈α
high-level
␈↓ ↓H␈↓notation␈α
for␈α
algorithms␈α
is␈α
beneficial␈α
in␈α
explaining␈α
and␈α
understanding␈α
complex␈α
algorithms.␈α The␈α
use
␈↓ ↓H␈↓of␈α∀abstract␈α∀data␈α∀structures␈α∪and␈α∀abstract␈α∀LISP␈α∀programs␈α∪shows␈α∀the␈α∀true␈α∀intent␈α∀of␈α∪structured
␈↓ ↓H␈↓programming␈α⊂and␈α⊂step-wise␈α⊂refinement.␈α⊂Much␈α⊃of␈α⊂the␈α⊂current␈α⊂work␈α⊂in␈α⊂mathematical␈α⊃theories␈α⊂of
␈↓ ↓H␈↓computation␈αis␈αbased␈αon␈αLISP-like␈αlanguages.␈αThus␈αLISP␈αis␈αa␈αformalism␈αfor␈αdescribing␈αalgorithms,
␈↓ ↓H␈↓for␈α∂writing␈α∂programs,␈α⊂and␈α∂for␈α∂proving␈α⊂properties␈α∂of␈α∂algorithms.␈α∂ We␈α⊂use␈α∂data␈α∂structures␈α⊂as␈α∂the
␈↓ ↓H␈↓main␈αthread␈αin␈αour␈αdiscussions␈αbecause␈αa␈αproper␈αappreciation␈αof␈αdata␈αstructures␈αas␈αabstract␈αobjects
␈↓ ↓H␈↓is a necessary prerequisite to an understanding of modern computer science.

␈↓ ↓H␈↓The␈α⊗importance␈α∃of␈α⊗abstraction␈α∃obviously␈α⊗goes␈α∃much␈α⊗further␈α∃than␈α⊗its␈α∃appearance␈α⊗in␈α∃LISP.
␈↓ ↓H␈↓Abstraction␈α⊂has␈α⊂often␈α⊂been␈α⊂used␈α⊂in␈α⊂other␈α⊂disciplines␈α⊂as␈α⊂a␈α⊂means␈α⊂for␈α⊂controlling␈α⊃complexity.␈α⊂In
␈↓ ↓H␈↓mathematics,␈α∞we␈α∞frequently␈α∞are␈α∞able␈α∞to␈α∞gain␈α∞new␈α∞insights␈α∞by␈α∞recasting␈α∞a␈α∂particularly␈α∞intransigent
␈↓ ↓H␈↓problem␈αin␈αa␈αmore␈αgeneral␈αsetting.␈α Similarly,␈αthe␈αintent␈αof␈αan␈αalgorithm␈αexpressed␈αin␈α
a␈αhigh-level
␈↓ ↓H␈↓language␈α⊂like␈α⊃Fortran␈α⊂or␈α⊃PL/1␈α⊂is␈α⊂more␈α⊃readily␈α⊂apparent␈α⊃than␈α⊂its␈α⊃machine-language␈α⊂equivalent.
␈↓ ↓H␈↓These␈α
are␈αboth␈α
examples␈αof␈α
the␈α
use␈αof␈α
abstraction.␈αOur␈α
use␈α
of␈αabstraction␈α
will␈αimpinge␈α
on␈αboth␈α
the
␈↓ ↓H␈↓mathematical␈αand␈αthe␈αprogramming␈αaspects.␈α Initially,␈αwe␈αwill␈αtalk␈αabout␈αdata␈αstructures␈αas␈αabstract
␈↓ ↓H␈↓objects␈αjust␈αas␈αthe␈αmathematician␈αtakes␈αthe␈αnatural␈αnumbers␈αas␈αabstract␈αentities.␈αWe␈αwill␈αattempt␈αto
␈↓ ↓H␈↓categorize␈α∂properties␈α∂common␈α⊂to␈α∂data␈α∂structures␈α∂and␈α⊂introduce␈α∂notation␈α∂for␈α⊂describing␈α∂functions
␈↓ ↓H␈↓defined␈αon␈α
these␈αabstractions.␈αAt␈α
this␈αlevel␈α
of␈αdiscussion␈αwe␈α
are␈αthinking␈α
of␈αour␈αLISP-like␈α
language
␈↓ ↓H␈↓primarily␈α
as␈αa␈α
notational␈α
convenience␈αrather␈α
than␈αa␈α
computational␈α
device.␈α However,␈α
after␈αa␈α
certain
␈↓ ↓H␈↓mathematical␈α∪familiarity␈α∪has␈α∪been␈α∪established␈α∪it␈α∀is␈α∪important␈α∪to␈α∪look␈α∪at␈α∪our␈α∪work␈α∀from␈α∪the
␈↓ ↓H␈↓viewpoint␈α
of␈α
computer␈α∞science.␈α
Here␈α
we␈α∞must␈α
think␈α
of␈α
the␈α∞computational␈α
aspects␈α
of␈α∞our␈α
notation.
␈↓ ↓H␈↓␈↓↓2  Introduction␈↓ E1.␈↓


␈↓ ↓H␈↓We␈α∞must␈α∞be␈α∞concerned␈α∞with␈α∞the␈α∞representational␈α∞problems:␈α∞implementation␈α∞on␈α∞realistic␈α∞machines,
␈↓ ↓H␈↓and␈α
efficiency␈αof␈α
algorithms␈α
and␈αdata␈α
structures.␈αHowever,␈α
it␈α
cannot␈αbe␈α
over-emphasized␈α
that␈αour
␈↓ ↓H␈↓need␈α∂for␈α∂understanding␈α∂is␈α∞best␈α∂served␈α∂at␈α∂the␈α∞higher␈α∂level␈α∂of␈α∂abstraction;␈α∞it␈α∂is␈α∂only␈α∂after␈α∂a␈α∞clear
␈↓ ↓H␈↓understanding␈αof␈αthe␈αproblem␈αis␈αattained␈αthat␈αwe␈αshould␈αbegin␈αthinking␈αabout␈αrepresentation.␈α We
␈↓ ↓H␈↓can␈α⊂exploit␈α⊃the␈α⊂analogy␈α⊂with␈α⊃traditional␈α⊂mathematics␈α⊂a␈α⊃bit␈α⊂further.␈α⊂ When␈α⊃we␈α⊂write␈α⊃␈↓αsqrt(x)␈↓␈α⊂in
␈↓ ↓H␈↓Fortran,␈αfor␈αexample,␈αwe␈αare␈αinitially␈αonly␈αconcerned␈αwith␈α␈↓αsqrt␈↓␈αas␈αa␈αmathematical␈αfunction␈αdefined
␈↓ ↓H␈↓such␈α
that␈α
␈↓αx = sqrt(x)*sqrt(x)␈↓.␈α
We␈α
are␈α
not␈α
interested␈α
in␈α
the␈α
specific␈α
algorithm␈α
used␈α
to␈αapproximate
␈↓ ↓H␈↓the␈α∂function␈α∂intended␈α∂in␈α∂the␈α∂notation.␈α∂Indeed,␈α∞thought␈α∂of␈α∂as␈α∂a␈α∂mathematical␈α∂notation,␈α∂it␈α∞doesn't
␈↓ ↓H␈↓matter␈αhow␈α␈↓αsqrt␈↓␈αis␈αcomputed.␈αWe␈αmight␈αwish␈αto␈αprove␈αsome␈αproperties␈αof␈αthe␈αalgorithm␈α
which␈αwe
␈↓ ↓H␈↓are␈α∞encoding.␈α∞ If␈α∞so,␈α∞we␈α∞would␈α∞only␈α∞use␈α∞the␈α∞mathematical␈α∞properties␈α∞of␈α∞the␈α∞idealized␈α∞square␈α
root
␈↓ ↓H␈↓function.␈α
Only␈αlater,␈α
after␈αwe␈α
had␈αconvinced␈α
ourselves␈αof␈α
the␈αcorrect␈α
encoding␈αof␈α
our␈α
intention␈αin
␈↓ ↓H␈↓the␈α~Fortran␈α~program,␈α~would␈α→we␈α~worry␈α~about␈α~the␈α→computational␈α~aspects␈α~of␈α~the␈α→Fortran
␈↓ ↓H␈↓implementation␈α∂␈↓αsqrt␈↓.␈α∂Indeed,␈α∞the␈α∂typical␈α∂user␈α∞will␈α∂never␈α∂proceed␈α∞deeper␈α∂into␈α∂the␈α∞representational
␈↓ ↓H␈↓mire␈α∪than␈α∪this;␈α∪only␈α∪if␈α∪his␈α∪algorithm␈α∪is␈α∪lethargic␈α∪due␈α∪to␈α∪inefficiencies,␈α∪or␈α∪inaccurate␈α∀due␈α∪to
␈↓ ↓H␈↓uncooperative approximations, will he look at the actual implementation of ␈↓αsqrt␈↓.

␈↓ ↓H␈↓Thus,␈αjust␈αas␈α
it␈αis␈αunnecessary␈α
to␈αlearn␈αmachine␈α
language␈αto␈αstudy␈α
numerical␈αalgorithms,␈αit␈α
is␈αalso
␈↓ ↓H␈↓unnecessary␈α∂to␈α∂learn␈α∞machine␈α∂language␈α∂to␈α∂understand␈α∞non-numerical␈α∂or␈α∂data␈α∂structure␈α∞processes.
␈↓ ↓H␈↓The␈α∂distinction␈α∂we␈α∂are␈α∂making␈α∂is␈α∂between␈α∂data␈α∂structures␈α∂and␈α∂storage␈α∂structures.␈α∂ That␈α⊂is,␈α∂data
␈↓ ↓H␈↓structures␈α∩are␈α∪independent␈α∩of␈α∪␈↓¬how␈↓␈α∩they␈α∪are␈α∩implemented␈α∪on␈α∩a␈α∪machine.␈α∩ Data␈α∪structures␈α∩are
␈↓ ↓H␈↓representations␈α⊂of␈α⊂information␈α⊃chosen␈α⊂to␈α⊂exhibit␈α⊃certain␈α⊂ordering␈α⊂and␈α⊃accessibility␈α⊂relationships
␈↓ ↓H␈↓between␈α
data␈α
items.␈α Certainly␈α
in␈α
the␈α
final␈αanalysis␈α
we␈α
cannot␈α
ignore␈αstorage␈α
structures␈α
when␈αwe␈α
are
␈↓ ↓H␈↓deciding␈α∞upon␈α
the␈α∞data␈α
structures␈α∞which␈α
will␈α∞encode␈α
the␈α∞algorithm,␈α
but␈α∞the␈α
interesting␈α∞aspects␈α
of
␈↓ ↓H␈↓representation␈α⊃of␈α⊃information␈α⊃can␈α⊃be␈α⊃discussed␈α⊃at␈α⊃the␈α⊃level␈α⊃of␈α⊃data␈α⊃structures␈α⊃with␈α⊃no␈α⊃loss␈α⊃of
␈↓ ↓H␈↓generality.␈α⊗ The␈α⊗mapping␈α⊗of␈α⊗data␈α⊗structures␈α∃to␈α⊗storage␈α⊗structures␈α⊗is␈α⊗usually␈α⊗quite␈α∃machine
␈↓ ↓H␈↓dependent␈αanyway,␈α
and␈αconsists␈αof␈α
bit-pushing,␈αtrickery␈α
and␈αblack␈αmagic.␈α
 We␈αare␈α
more␈αinterested
␈↓ ↓H␈↓in␈α∞ideas␈α∞than␈α∞coding␈α∞tricks.␈α
 We␈α∞will␈α∞see␈α∞that␈α∞it␈α∞is␈α
possible,␈α∞and␈α∞most␈α∞beneficial,␈α∞to␈α∞structure␈α
our
␈↓ ↓H␈↓programs␈α
such␈α
that␈αthere␈α
is␈α
a␈α
very␈αclean␈α
interface␈α
between␈α
the␈αabstract␈α
algorithm␈α
and␈α
the␈αchosen
␈↓ ↓H␈↓representation.␈α
 That␈α
is,␈α
there␈αwill␈α
be␈α
a␈α
set␈α
of␈αrepresentation-manipulating␈α
programs␈α
to␈α
test,␈αselect␈α
or
␈↓ ↓H␈↓construct␈αelements␈αof␈αthe␈αdomain;␈αand␈αthere␈αwill␈αbe␈αa␈αprogram␈αencoding␈αthe␈αalgorithm.␈αTo␈αchange
␈↓ ↓H␈↓representations␈α⊃only␈α⊃requires␈α⊃changes␈α⊃to␈α∩constructors,␈α⊃selectors␈α⊃and␈α⊃predicates,␈α⊃not␈α⊃to␈α∩the␈α⊃basic
␈↓ ↓H␈↓program.

␈↓ ↓H␈↓Thus,␈α∩we␈α∪will␈α∩use␈α∪abstraction␈α∩as␈α∩a␈α∪two-edged␈α∩sword␈α∪to␈α∩control␈α∩complexity.␈α∪ We␈α∩will␈α∪use␈α∩the
␈↓ ↓H␈↓notational␈α∞benefits␈α∞to␈α∞express␈α∞our␈α
ideas␈α∞in␈α∞a␈α∞high-level␈α∞form;␈α
we␈α∞will␈α∞study␈α∞the␈α∞properties␈α∞of␈α
the
␈↓ ↓H␈↓notation␈α⊂as␈α⊂a␈α⊂computational␈α⊂device␈α⊃for␈α⊂describing␈α⊂an␈α⊂algorithm.␈α⊂ One␈α⊂important␈α⊃insight␈α⊂which
␈↓ ↓H␈↓should␈α∪be␈α∪cultivated␈α∪in␈α∪this␈α∪process␈α∪is␈α∪the␈α∪distinction␈α∪between␈α∪the␈α∪concepts␈α∪of␈α∪function␈α∩and
␈↓ ↓H␈↓algorithm.␈α The␈αidea␈α
of␈αfunction␈αis␈αmathematical␈α
and␈αis␈αindependent␈α
of␈αany␈αnotion␈αof␈α
computation;
␈↓ ↓H␈↓the␈α∩meaning␈α∩of␈α∪"algorithm"␈α∩is␈α∩computational,␈α∪the␈α∩effect␈α∩of␈α∩an␈α∪algorithm␈α∩being␈α∩to␈α∪compute␈α∩a
␈↓ ↓H␈↓function. Thus there are typically many algorithms which will compute a specific function.

␈↓ ↓H␈↓This␈αtext␈αis␈α␈↓↓not␈↓␈αmeant␈αto␈αbe␈αa␈αprogramming␈αmanual␈αfor␈αLISP.␈α A␈αcertain␈αamount␈αof␈αtime␈αis␈αspent
␈↓ ↓H␈↓giving␈α
insights␈α
into␈α
techniques␈α
for␈α
writing␈α
LISP␈α
functions.␈α
 There␈α
are␈α
two␈α
reasons␈α
for␈α∞this.␈α
First,
␈↓ ↓H␈↓the␈α
style␈αof␈α
LISP␈α
programming␈αis␈α
quite␈α
different␈αfrom␈α
that␈α
of␈α"normal"␈α
programming.␈α
 LISP␈αwas
␈↓ ↓H␈↓one␈α
of␈α
the␈α
first␈α
languages␈α
to␈α
exploit␈αthe␈α
virtues␈α
of␈α
recursive␈α
programming␈α
and␈α
explore␈α
the␈αpower␈α
of
␈↓ ↓H␈↓␈↓↓1.␈↓ 	mIntroduction     3␈↓


␈↓ ↓H␈↓function-valued␈α∪variables.␈α∪ Second,␈α∪and␈α∪more␈α∪important,␈α∀we␈α∪will␈α∪spend␈α∪a␈α∪great␈α∪deal␈α∀of␈α∪time
␈↓ ↓H␈↓discussing␈α⊃various␈α⊃levels␈α⊂of␈α⊃implementation␈α⊃of␈α⊂the␈α⊃language.␈α⊃LISP␈α⊂is␈α⊃an␈α⊃excellent␈α⊃medium␈α⊂for
␈↓ ↓H␈↓introducing␈αstandard␈αtechniques␈αin␈αdata␈αstructure␈αmanipulation.␈αTechniques␈αfor␈αimplementation␈αof
␈↓ ↓H␈↓recursion,␈α∀implementation␈α∀of␈α∀complex␈α∀data␈α∪structures,␈α∀storage␈α∀management,␈α∀and␈α∀symbol␈α∪table
␈↓ ↓H␈↓manipulation␈α∩are␈α∩easily␈α∩motivated␈α∩in␈α∩the␈α∩context␈α∩of␈α∩language␈α∩implementation.␈α∩Many␈α∪of␈α∩these
␈↓ ↓H␈↓standard␈α⊂techniques␈α∂first␈α⊂arose␈α∂in␈α⊂the␈α∂implementation␈α⊂of␈α∂LISP.␈α⊂But␈α∂it␈α⊂is␈α∂pointless␈α⊂to␈α⊂attempt␈α∂a
␈↓ ↓H␈↓discussion of implementation unless the reader has a thorough grasp of the language.

␈↓ ↓H␈↓Granting␈α∂the␈α∞efficacy␈α∂of␈α∞our␈α∂endeavor␈α∞in␈α∂abstraction,␈α∞why␈α∂study␈α∞LISP?␈α∂ LISP␈α∞is␈α∂at␈α∂least␈α∞fifteen
␈↓ ↓H␈↓years␈α∞old␈α∞and␈α∞many␈α∞languages␈α∞now␈α
offer␈α∞themselves␈α∞with␈α∞better␈α∞notation,␈α∞more␈α∞efficient␈α
running
␈↓ ↓H␈↓code,␈α∞or␈α∞larger␈α∞varieties␈α∞of␈α∞data␈α∞structure.␈α∞ The␈α∞difficulty␈α∞is␈α∞that␈α∞the␈α∞appropriate␈α∞combination␈α
of
␈↓ ↓H␈↓these features is not present in any other language.

␈↓ ↓H␈↓As␈αa␈αprogramming␈αlanguage,␈αthere␈αis␈αonly␈αone␈αviable␈αalternative␈αto␈αLISP␈αif␈αwe␈αwish␈αto␈α
cover␈αthis
␈↓ ↓H␈↓broad␈αscope␈αof␈αtopics␈αin␈αa␈αunified␈αapproach:␈αinvent␈αour␈αown␈αlanguage.␈α Toy␈αlanguages␈αare␈αsuspect
␈↓ ↓H␈↓for␈αseveral␈αreasons.␈αThe␈αstudent␈αmay␈αsuspect␈α(usually␈αfor␈αgood␈αreason)␈αthat␈αhe␈αis␈αa␈αsubject␈αin␈αa␈αnot
␈↓ ↓H␈↓too␈α∞clever␈α∞experiment␈α∞being␈α∞performed␈α∞upon␈α∂him␈α∞by␈α∞his␈α∞instructor.␈α∞Having␈α∞a␈α∞backlog␈α∂of␈α∞fifteen
␈↓ ↓H␈↓years␈α∩in␈α∩experience␈α∩and␈α∩example␈α∩programs␈α∩should␈α∩do␈α∩much␈α∩to␈α∩subdue␈α∩this␈α∪discomfort.␈α∩ The
␈↓ ↓H␈↓development␈α
of␈α
LISP␈α
also␈α
shows␈α
many␈α
of␈α
the␈α
mistakes␈α
that␈α
the␈α
original␈α
implementors␈α
and␈α
designers
␈↓ ↓H␈↓made.  We will point out the flaws and pitfalls awaiting the unwary language designer.

␈↓ ↓H␈↓We␈α⊂claim␈α∂the␈α⊂more␈α∂interesting␈α⊂aspects␈α∂of␈α⊂LISP␈α∂for␈α⊂students␈α∂of␈α⊂Computer␈α∂Science␈α⊂lie␈α∂not␈α⊂in␈α∂its
␈↓ ↓H␈↓features␈α∞as␈α∞a␈α∞programming␈α∞language,␈α∞but␈α∞in␈α∂what␈α∞it␈α∞can␈α∞show␈α∞about␈α∞the␈α∞␈↓↓structure␈↓␈α∂of␈α∞Computer
␈↓ ↓H␈↓Science.␈α∞ There␈α∂is␈α∞a␈α∞rapidly␈α∂expanding␈α∞body␈α∂of␈α∞knowledge␈α∞unique␈α∂to␈α∞Computer␈α∂Science,␈α∞neither
␈↓ ↓H␈↓mathematical␈α⊂nor␈α⊂engineering␈α⊂per␈α⊂se.␈α⊂ Much␈α⊂of␈α∂this␈α⊂area␈α⊂is␈α⊂presented␈α⊂most␈α⊂clearly␈α⊂by␈α∂studying
␈↓ ↓H␈↓LISP.

␈↓ ↓H␈↓Again␈αthere␈αare␈αtwo␈αways␈αto␈αlook␈αat␈αa␈αhigh␈αlevel␈αlanguage:␈αas␈αa␈αmathematical␈αformalism,␈αand␈αas␈αa
␈↓ ↓H␈↓programming␈α
language.␈α
 LISP␈α
is␈α
a␈α
better␈α∞formalism␈α
than␈α
most␈α
of␈α
its␈α
mathematical␈α∞rivals␈α
because
␈↓ ↓H␈↓there␈α∞is␈α∞sufficient␈α∞organizational␈α∞complexity␈α∞present␈α∞in␈α∞LISP␈α∞so␈α∞as␈α∞to␈α∞make␈α∞its␈α∂implementation␈α∞a
␈↓ ↓H␈↓realistic␈α∂computer␈α⊂science␈α∂task␈α∂and␈α⊂not␈α∂just␈α⊂an␈α∂interesting␈α∂mathematical␈α⊂curiosity.␈α∂ Much␈α⊂of␈α∂the
␈↓ ↓H␈↓power␈α∩of␈α∩LISP␈α∩lies␈α∪in␈α∩its␈α∩simplicity.␈α∩ The␈α∩data␈α∪structures␈α∩are␈α∩rich␈α∩enough␈α∩to␈α∪easily␈α∩describe
␈↓ ↓H␈↓sophisticated␈α∩algorithms␈α∩but␈α∩not␈α∩so␈α∩rich␈α∩as␈α⊃to␈α∩become␈α∩obfuscatory.␈α∩ Most␈α∩every␈α∩aspect␈α∩of␈α⊃the
␈↓ ↓H␈↓implementation␈α∞of␈α∞LISP␈α∞and␈α∞its␈α∂translators␈α∞has␈α∞immediate␈α∞implications␈α∞to␈α∞the␈α∂implementation␈α∞of
␈↓ ↓H␈↓other languages and to the design of programming languages in general.

␈↓ ↓H␈↓We␈α
will␈αdescribe␈α
language␈α
translators␈α(interpreters␈α
and␈α
compilers)␈αas␈α
LISP␈α
functions.␈α The␈α
structure
␈↓ ↓H␈↓of␈αthese␈αtranslators␈αwhen␈αexposed␈αas␈αLISP␈αfunctions␈αaids␈αimmensely␈αin␈αunderstanding␈αthe␈αessential
␈↓ ↓H␈↓character␈α∂of␈α∂such␈α∂translators.␈α∂ This␈α∞is␈α∂partly␈α∂due␈α∂to␈α∂the␈α∞simplicity␈α∂of␈α∂the␈α∂language,␈α∂but␈α∞perhaps
␈↓ ↓H␈↓more␈αdue␈αto␈αour␈αability␈αto␈αgo␈αright␈αto␈αthe␈αessential␈αtranslating␈αalgorithm␈αwithout␈αbecoming␈αbogged
␈↓ ↓H␈↓down in details of syntax.

␈↓ ↓H␈↓LISP␈αhas␈αvery␈αimportant␈αimplications␈αin␈α
the␈αfield␈αof␈αprogramming␈αlanguage␈αsemantics,␈αand␈α
is␈αthe
␈↓ ↓H␈↓dominant␈αlanguage␈αin␈αthe␈αclosely␈αrelated␈αstudy␈αof␈αprovability␈αof␈αproperties␈αof␈αprograms.␈α The␈αidea
␈↓ ↓H␈↓of␈α⊃proving␈α⊃properties␈α⊂of␈α⊃programs␈α⊃has␈α⊂been␈α⊃around␈α⊃for␈α⊂a␈α⊃very␈α⊃long␈α⊂time.␈α⊃Goldstine␈α⊃and␈α⊂von
␈↓ ↓H␈↓␈↓↓4  Introduction␈↓ E1.␈↓


␈↓ ↓H␈↓Neumann␈αwere␈αaware␈αof␈αthe␈αpractical␈αbenefits␈α
of␈αsuch␈αendeavors.␈αJ.␈αMcCarthy's␈αwork␈αin␈αLISP␈α
and
␈↓ ↓H␈↓the␈α∂Theory␈α∂of␈α∞Computation␈α∂sought␈α∂to␈α∞establish␈α∂formalisms␈α∂and␈α∞rules␈α∂of␈α∂inference␈α∂for␈α∞reasoning
␈↓ ↓H␈↓about␈α
programs.␈α
 However,␈α
the␈α
working␈α
programmers␈αrecognized␈α
debugging␈α
as␈α
the␈α
only␈α
tool␈αwith
␈↓ ↓H␈↓which␈αto␈αgenerate␈αa␈α"correct"␈αprogram,␈αthough␈αclearly␈αthe␈αnon-occurence␈αof␈αbugs␈αis␈αno␈αguarantee␈α
of
␈↓ ↓H␈↓correctness␈α⊃␈↓π 1␈↓.␈α⊃ The␈α⊂sad␈α⊃state␈α⊃of␈α⊃affairs␈α⊂is␈α⊃that␈α⊃the␈α⊃programmer␈α⊂was␈α⊃right.␈α⊃Until␈α⊃very␈α⊂recently
␈↓ ↓H␈↓techniques for establishing correctness of practical programs simply did not exist.

␈↓ ↓H␈↓A recent set of events is beginning to change this.

␈↓ ↓H␈↓␈↓↓1.␈↓␈α⊃Programs␈α⊃are␈α⊃becoming␈α⊃so␈α⊃large␈α⊃and␈α⊃complex␈α⊃that,␈α⊃even␈α⊃though␈α⊃we␈α⊃write␈α∩in␈α⊃a
␈↓ ↓H␈↓␈↓ αhhigh-level␈αlanguage,␈α
our␈αintuitions␈αare␈α
not␈αsufficient␈αto␈α
sustain␈αus␈αwhen␈α
we
␈↓ ↓H␈↓␈↓ αhtry to find bugs. We are literally being forced to look beyond debugging.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α
The␈α
formalisms␈αare␈α
maturing.␈α
We␈α
know␈αa␈α
lot␈α
more␈αabout␈α
how␈α
to␈α
write␈α"structured
␈↓ ↓H␈↓␈↓ αhprograms";␈α∞we␈α∞know␈α∞how␈α∂to␈α∞design␈α∞languages␈α∞whose␈α∞constructs␈α∂are␈α∞more
␈↓ ↓H␈↓␈↓ αhamenable␈α∞to␈α∂proof␈α∞techniques.␈α∂ And␈α∞most␈α∞importantly,␈α∂the␈α∞tools␈α∂we␈α∞need
␈↓ ↓H␈↓␈↓ αhfor expressing properties of programs are finally being developed.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α
The␈α
development␈α
of␈α
on-line␈α
techniques.␈α
The␈α
on-line␈α
system␈α
is␈α
the␈α
only␈α∞reason␈α
that
␈↓ ↓H␈↓␈↓ αhthe␈α→traditional␈α_means␈α→of␈α→construction␈α_and␈α→modification␈α→of␈α_complex
␈↓ ↓H␈↓␈↓ αhprograms␈α⊂and␈α⊂systems␈α∂has␈α⊂been␈α⊂able␈α∂to␈α⊂survive␈α⊂this␈α⊂long.␈α∂Sophisticated
␈↓ ↓H␈↓␈↓ αhdisplay␈α
editors,␈αdebuggers␈α
and␈α
file␈αhandlers␈α
have␈α
kept␈αus␈α
from␈αfalling␈α
over
␈↓ ↓H␈↓␈↓ αhthe␈α∪edge.␈α∩The␈α∪use␈α∩of␈α∪these␈α∩on-line␈α∪devices␈α∩in␈α∪an␈α∪interactive␈α∩program
␈↓ ↓H␈↓␈↓ αhconstructing␈α∀system␈α∀should␈α∀allow␈α∪us␈α∀to␈α∀actually␈α∀write␈α∀correct␈α∪practical
␈↓ ↓H␈↓␈↓ αhprograms.

␈↓ ↓H␈↓This␈αenlightened␈αview␈αof␈αprogramming␈αblends␈αwell␈αwith␈αthe␈αLISP␈αphilosophy.␈α We␈αwill␈αshow␈αthat
␈↓ ↓H␈↓the␈α
most␈α
natural␈α
way␈αto␈α
write␈α
LISP␈α
programs␈αis␈α
"structured"␈α
in␈α
the␈αbest␈α
sense␈α
of␈α
the␈α
word,␈αbeing
␈↓ ↓H␈↓clean␈α∪in␈α∪control␈α∩structure,␈α∪concise␈α∪by␈α∩not␈α∪attempting␈α∪to␈α∩do␈α∪too␈α∪much,␈α∩and␈α∪independent␈α∪of␈α∩a
␈↓ ↓H␈↓particular data representation.

␈↓ ↓H␈↓Many␈α
of␈α
the␈α
existing␈α
techniques␈α
for␈α
establishing␈α
correctness␈α
originated␈α
in␈αMcCarthy's␈α
investigations
␈↓ ↓H␈↓of␈αLISP;␈αand␈αsome␈αvery␈αrecent␈αwork␈αon␈αmathematical␈αmodels␈αfor␈αprogramming␈αlanguages␈αis␈αeasily
␈↓ ↓H␈↓motivated from a discussion of LISP.

␈↓ ↓H␈↓Finally␈αthere␈αare␈αcertain␈αproperties␈αof␈αLISP-like␈αlanguages␈αwhich␈αmake␈αthem␈αthe␈αnatural␈αcandidate
␈↓ ↓H␈↓for␈αinteractive␈αprogram␈α
specification.␈α In␈αthe␈α
chapter␈αon␈αimplications␈α
of␈αLISP␈αwe␈α
will␈αcharacterize
␈↓ ↓H␈↓"LISP-like" and show how interactive methods can be developed.

␈↓ ↓H␈↓This␈α∞text␈α∞is␈α
primarily␈α∞designed␈α∞for␈α
undergraduates␈α∞and␈α∞therefore␈α
an␈α∞attempt␈α∞is␈α
made␈α∞to␈α∞make␈α
it
␈↓ ↓H␈↓self-contained.␈αThere␈αare␈αbasically␈αfive␈αareas␈αin␈αwhich␈αto␈αpartition␈αthe␈αtopics:␈αthe␈αmechanics␈αof␈αthe
␈↓ ↓H␈↓language,␈αthe␈αevaluation␈αof␈α
expressions␈αin␈αLISP,␈αthe␈αstatic␈α
structure␈αof␈αLISP,␈αthe␈αdynamic␈α
structure
␈↓ ↓H␈↓of␈α
LISP,␈α
and␈α
the␈α
efficient␈α
representation␈α∞of␈α
data␈α
structures␈α
and␈α
algorithms.␈α
 Each␈α
area␈α∞builds␈α
on

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 1␈↓ In truth, it usually meant that no one was using the program.
␈↓ ↓H␈↓␈↓↓1.␈↓ 	mIntroduction     5␈↓


␈↓ ↓H␈↓the␈α
previous.␈α
Taken␈αas␈α
a␈α
group␈α
these␈αtopics␈α
introduce␈α
much␈α
of␈αwhat␈α
is␈α
interesting␈αcomputer␈α
science.
␈↓ ↓H␈↓The␈α∩first␈α∩area␈α∩develops␈α∪the␈α∩programming␈α∩philosophy␈α∩of␈α∩LISP:␈α∪the␈α∩use␈α∩of␈α∩data␈α∪structures␈α∩in
␈↓ ↓H␈↓programming;␈α
the␈α∞language␈α
constructs␈α
of␈α∞recursion;␈α
and␈α
other␈α∞uncommon␈α
control␈α∞structures.␈α
 The
␈↓ ↓H␈↓second␈α
area␈αinvolves␈α
a␈α
careful␈αstudy␈α
of␈αthe␈α
meaning␈α
of␈αevaluation␈α
in␈α
LISP␈αgives␈α
insights␈αinto␈α
other
␈↓ ↓H␈↓languages␈α∞and␈α∞to␈α∞the␈α∞general␈α∞question␈α∞of␈α
implementation.␈α∞The␈α∞next␈α∞two␈α∞areas␈α∞are␈α∞involved␈α
with
␈↓ ↓H␈↓implementation.␈α
The␈αsection␈α
on␈αstatic␈α
structure␈αdeals␈α
with␈αthe␈α
basic␈αorgainzation␈α
of␈αmemory␈α
for␈αa
␈↓ ↓H␈↓LISP␈α⊂machine -- be␈α∂it␈α⊂hardware␈α⊂or␈α∂simulated␈α⊂in␈α∂software.␈α⊂The␈α⊂dynamics␈α∂of␈α⊂LISP␈α⊂discusses␈α∂the
␈↓ ↓H␈↓primitive␈α∀control␈α∀structures␈α∪necessary␈α∀for␈α∀implementation␈α∀of␈α∪the␈α∀LISP␈α∀control␈α∀structures␈α∪and
␈↓ ↓H␈↓procedure␈α
calls.␈α
LISP␈α
compilers␈α
are␈α
discussed␈α
here.␈α
 The␈α
final␈α
section␈α
relates␈α
our␈α
discussion␈αof␈α
LISP
␈↓ ↓H␈↓and␈αits␈αimplementation␈αto␈αthe␈αmore␈αtraditional␈αmaterial␈αof␈αa␈αdata␈αstructures␈αcourse.␈αWe␈αdiscuss␈αthe
␈↓ ↓H␈↓problems␈α∂of␈α∂efficient␈α∂representation␈α∂of␈α∂data␈α∂structures.␈α∂By␈α∂this␈α∂point␈α∂the␈α∂student␈α∂should␈α∂have␈α∞a
␈↓ ↓H␈↓better␈α⊂understanding␈α⊂of␈α⊂the␈α⊂uses␈α⊂of␈α⊂data␈α⊂structures␈α⊂and␈α⊂should␈α⊂be␈α⊂motivated␈α⊂to␈α⊃examine␈α⊂these
␈↓ ↓H␈↓issues.

␈↓ ↓H␈↓A␈α
large␈αcollection␈α
of␈αproblems␈α
has␈αbeen␈α
included.␈αThe␈α
reader␈αis␈α
urged␈αto␈α
do␈αas␈α
many␈α
as␈αpossible.
␈↓ ↓H␈↓The␈αproblems␈α
are␈αmostly␈α
non-trivial;␈αthey␈α
attempt␈αto␈α
be␈αrealistic,␈α
introducing␈αsome␈αnew␈α
information
␈↓ ↓H␈↓which␈α
the␈α
readers␈α
should␈α∞be␈α
able␈α
to␈α
discover␈α
themselves.␈α∞There␈α
are␈α
also␈α
a␈α
few␈α∞rather␈α
substantial
␈↓ ↓H␈↓projects.␈α At␈αleast␈α
one␈αshould␈αbe␈αattempted.␈α
 There␈αis␈αa␈αsignificant␈α
difference␈αbetween␈αbeing␈αable␈α
to
␈↓ ↓H␈↓program small problems and being able to handle large projects.

␈↓ ↓H␈↓Finally␈α∞a␈α
note␈α∞on␈α
the␈α∞structure␈α
of␈α∞the␈α
text.␈α∞The␈α
emphasis␈α∞flows␈α
from␈α∞the␈α
abstract␈α∞to␈α∞the␈α
specific,
␈↓ ↓H␈↓beginning␈αwith␈α
a␈αprecise␈αdescription␈α
of␈αthe␈αdomain␈α
of␈αLISP␈αfunctions␈α
and␈αthe␈α
operations␈αdefined
␈↓ ↓H␈↓over␈αthat␈αdomain,␈αand␈αmoves␈α
to␈αa␈αdiscussion␈αof␈αthe␈α
details␈αof␈αefficient␈αimplementation␈αof␈α
LISP-like
␈↓ ↓H␈↓languages.␈α The␈αpractical-minded␈αprogrammer␈αmight␈αbe␈αput-off␈αby␈αthe␈α"irrelevant"␈αtheory␈αand␈αthe
␈↓ ↓H␈↓theoretical-minded␈αmathematician␈αmight␈αbe␈αput-off␈αby␈αthe␈α"irrelevant"␈αdetails␈αof␈αimplementation.␈αIf
␈↓ ↓H␈↓you lie somewhere between these two extremes, then welcome.
␈↓ ↓H␈↓␈↓↓6  Symbolic expressions␈↓ C2.␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 2

␈↓ ↓H␈↓↓␈↓ ¬∂SYMBOLIC EXPRESSIONS␈↓






␈↓ ↓H␈↓␈↓ ¬`␈↓↓2.1  Introduction␈↓


␈↓ ↓H␈↓This␈αbook␈αis␈α
a␈αstudy␈αof␈α
data␈αstructures␈αand␈αprogramming␈α
languages;␈αin␈αparticular␈α
it␈αis␈αa␈α
study␈αof
␈↓ ↓H␈↓data␈α
structures␈αand␈α
programming␈αlanguages␈α
centered␈αaround␈α
the␈αlanguage␈α
LISP.␈αHowever,␈α
this␈αis
␈↓ ↓H␈↓not␈αa␈αmanual␈αto␈αhelp␈αyou␈αbecome␈αa␈αproficient␈αLISP␈αcoder.␈α We␈αwill␈αstudy␈αmany␈αof␈αthe␈αformal␈αand
␈↓ ↓H␈↓theoretical␈αaspects␈αof␈αlanguages␈αand␈αdata␈αstructures␈αas␈αwell␈αas␈αexamining␈αthe␈αpractical␈αapplications
␈↓ ↓H␈↓of␈α
data␈α
structures.␈α
We␈α
will␈α
show␈α
that␈α
this␈α
area␈α
of␈α
computer␈α
science␈α
is␈α
a␈α
discipline␈α
of␈α
importance␈α
and
␈↓ ↓H␈↓beauty,␈α
worthy␈αof␈α
careful␈α
study.␈α How␈α
are␈α
we␈αto␈α
proceed?␈α
How␈αdo␈α
we␈α
introduce␈αrigor␈α
into␈α
a␈αfield
␈↓ ↓H␈↓whose␈αcountenance␈αis␈αas␈α␈↓αad␈αhoc␈↓␈αand␈αdiverse␈αas␈αthat␈αof␈αprogramming?␈α We␈αmust␈αalso␈αbear␈αin␈αmind
␈↓ ↓H␈↓that␈αthe␈α
results␈αof␈αour␈α
studies␈αare␈α
to␈αhave␈αpractical␈α
applications.␈α We␈αmust␈α
not␈αpursue␈α
theory␈αand
␈↓ ↓H␈↓rigor␈α
without␈αproper␈α
regard␈α
for␈αpractice.␈α
Our␈αstudy␈α
is␈α
not␈αthat␈α
of␈α
pure␈αmathematics;␈α
our␈αresults␈α
will
␈↓ ↓H␈↓have␈α↔applications␈α⊗in␈α↔everyday␈α⊗programming␈α↔practice.␈α⊗ However,␈α↔for␈α⊗guidance␈α↔let's␈α↔look␈α⊗at
␈↓ ↓H␈↓mathematics.␈α
Here␈α
is␈α
a␈α
well-established␈α
discipline␈α
rich␈αin␈α
history␈α
and␈α
full␈α
of␈α
results␈α
of␈αboth␈α
practical
␈↓ ↓H␈↓and theoretical importance.

␈↓ ↓H␈↓One␈α
of␈α
the␈α
more␈α
fertile,␈α
yet␈α
easily␈αintroduced␈α
areas␈α
of␈α
mathematics,␈α
is␈α
that␈α
of␈α
elementary␈αnumber
␈↓ ↓H␈↓theory.␈α⊃It␈α⊃is␈α∩easy␈α⊃to␈α⊃introduce␈α∩because␈α⊃everyone␈α⊃knows␈α∩something␈α⊃about␈α⊃the␈α∩natural␈α⊃numbers.
␈↓ ↓H␈↓Number␈α⊃theory␈α⊃studies␈α⊃properties␈α⊃of␈α⊂a␈α⊃certain␈α⊃class␈α⊃of␈α⊃operations␈α⊂definable␈α⊃over␈α⊃the␈α⊃set␈α⊃␈↓N␈↓␈α⊂of
␈↓ ↓H␈↓non-negative␈αintegers␈αalso␈αcalled␈αnatural␈αnumbers.␈α A␈αvery␈αformal␈αpresentation␈αmight␈αbegin␈αwith␈αa
␈↓ ↓H␈↓construction␈αof␈α␈↓N␈↓␈αfrom␈αmore␈αprimitive␈αnotions,␈αbut␈αit␈αis␈αusually␈αassumed␈αthat␈αthe␈αreader␈αis␈α
familiar
␈↓ ↓H␈↓with␈αthe␈αfundamental␈αproperties␈αof␈α␈↓N␈↓.␈α In␈αeither␈αcase␈αthe␈αnext␈αstep␈αwould␈αbe␈αto␈αdefine␈αthe␈αclass␈αof
␈↓ ↓H␈↓operations which we would allow on our domain.

␈↓ ↓H␈↓We␈α∞shall␈α∞begin␈α∞our␈α
study␈α∞of␈α∞LISP␈α∞in␈α∞a␈α
similar␈α∞manner,␈α∞as␈α∞an␈α
investigation␈α∞of␈α∞a␈α∞certain␈α∞class␈α
of
␈↓ ↓H␈↓operations␈αdefinable␈α
over␈αa␈αdomain␈α
of␈αobjects,␈αcalled␈α
Symbolic␈αExpressions.␈α Though␈α
most␈αpeople
␈↓ ↓H␈↓know␈α∂something␈α∂about␈α∂the␈α∂natural␈α∂numbers,␈α∂we␈α∂are␈α∂perhaps␈α∂not␈α∂so␈α∂fortunate␈α∂when␈α∂it␈α∂comes␈α∂to
␈↓ ↓H␈↓Symbolic␈α∩expressions.␈α∩We␈α∩must␈α∩define␈α∩what␈α∪we␈α∩mean␈α∩by␈α∩"symbolic␈α∩expression".␈α∩Let's␈α∪look␈α∩to
␈↓ ↓H␈↓mathematics␈αagain␈αfor␈αhelp.␈αIf␈αwe␈αasked␈αsomeone␈αto␈αdefine␈αthe␈αdomain␈α␈↓N␈↓,␈αthe␈αdefinition␈αwe␈αwould
␈↓ ↓H␈↓receive␈α⊃would␈α⊃depend␈α⊃on␈α⊃how␈α⊃familar␈α⊃that␈α⊃individual␈α⊃was␈α⊃with␈α⊃the␈α⊃properties␈α⊃of␈α⊃the␈α⊃natural
␈↓ ↓H␈↓numbers.

␈↓ ↓H␈↓For most people and purposes, the following characterization of a natural number is satisfactory:

␈↓ ↓H␈↓␈↓	I:␈↓␈↓ αXA natural number is a sequence of decimal digits.
␈↓ ↓H␈↓␈↓↓2.1␈↓ 	mIntroduction     7␈↓


␈↓ ↓H␈↓However␈α
this␈α
description␈α
is␈α
mathematically␈α∞quite␈α
superficial␈α
and␈α
is␈α
completely␈α
inadequate␈α∞for␈α
the
␈↓ ↓H␈↓purposes␈α∀of␈α∀discussing␈α∀properties␈α∀of␈α∀␈↓N␈↓.␈α∀ Clearly␈α∀all␈α∀of␈α∀the␈α∀information␈α∀we␈α∀know␈α∀about␈α∪the
␈↓ ↓H␈↓relationships␈αbetween␈αnatural␈αnumbers␈αis␈αlacking␈αin␈αthis␈αdescription.␈αThe␈α"meaning"␈αof␈αthe␈αnatural
␈↓ ↓H␈↓numbers␈α
is␈α
missing.␈α
It␈α
is␈α
like␈α
giving␈αa␈α
person␈α
an␈α
alphabet␈α
and␈α
rules␈α
for␈α
forming␈αsyntactically␈α
correct
␈↓ ↓H␈↓words but not supplying a dictionary which relates these words to the person's vocabulary.

␈↓ ↓H␈↓If pressed for details we might attempt a more adequate characterization like the following:

␈↓ ↓H␈↓␈↓ β(␈↓↓1.␈↓ ␈↓αzero␈↓ is an element of ␈↓N␈↓.

␈↓ ↓H␈↓␈↓	II:␈↓␈↓ β(␈↓↓2.␈↓ If ␈↓αn␈↓ is in ␈↓N␈↓ then  the ␈↓αsuccessor␈↓ of ␈↓αn␈↓ is in ␈↓N␈↓.


␈↓ ↓H␈↓␈↓ β8␈↓↓3.␈↓␈α
The␈α
only␈α
elements␈α∞of␈α
␈↓N␈↓␈α
are␈α
those␈α
created␈α∞by␈α
finitely␈α
many␈α
applications␈α∞of␈α
␈↓↓1.␈↓
␈↓ ↓H␈↓␈↓ β8and ␈↓↓2.␈↓.

␈↓ ↓H␈↓Definition␈α
␈↓	II␈↓␈αappears␈α
to␈α
be␈αcompletely␈α
at␈α
the␈αother␈α
end␈α
of␈αthe␈α
spectrum;␈α
it␈αtells␈α
us␈α
very␈αlittle␈α
about
␈↓ ↓H␈↓the␈αappearance␈αof␈αthe␈αintegers.␈αIt␈αgives␈αus␈αan␈αinitial␈αelement␈α␈↓αzero␈↓␈αand␈αa␈αmysterious␈αoperation␈αcalled
␈↓ ↓H␈↓␈↓αsuccessor␈↓,␈α
which␈αis␈α
supposed␈αto␈α
exhibit␈α
a␈αnew␈α
element,␈αgiven␈α
an␈α
old␈αone.␈α
 And␈αunless␈α
we␈αare␈α
careful
␈↓ ↓H␈↓about␈α∞the␈α∞meaning␈α∞of␈α∞␈↓αsuccessor␈↓,␈α∞definition␈α∞␈↓	II␈↓␈α
will␈α∞be␈α∞inadequate.␈α∞For␈α∞example␈α∞if␈α∞we␈α∞define␈α
the
␈↓ ↓H␈↓successor of a natural number to be that same number then ␈↓	II␈↓ is satisfied but unsatisfactory.

␈↓ ↓H␈↓How␈α
should␈α
we␈α
describe␈α␈↓αsuccessor␈↓␈α
so␈α
that␈α
our␈αintentions␈α
are␈α
captured?␈α
 A␈αsufficient␈α
way␈α
is␈α
to␈αgive␈α
a
␈↓ ↓H␈↓definition␈α∞of␈α∞␈↓αsuccessor␈↓␈α∞as␈α∞a␈α∞mapping␈α∞or␈α∞function␈α∞from␈α∞natural␈α∞numbers␈α∞to␈α∞natural␈α∞numbers.␈α∞ To
␈↓ ↓H␈↓give␈αsuch␈αan␈αexplicit␈αdefinition␈αrequires␈αsome␈αnotation:␈αlet␈α␈↓α0␈↓␈αbe␈αa␈αnotation␈αfor␈α␈↓αzero␈↓;␈αthen␈αwe␈αdefine
␈↓ ↓H␈↓a function ␈↓↓S␈↓ such that the successor of ␈↓αzero␈↓ -- called ␈↓αone␈↓ -- is denoted by ␈↓↓S␈↓α(0)␈↓↓, etc.

␈↓ ↓H␈↓The␈αcharacterization␈αof␈αdecimal␈αdigits␈αgiven␈αin␈α␈↓	I␈↓␈αis␈αsyntactic.␈α The␈αnotation␈αitself␈αtells␈α
us␈αnothing
␈↓ ↓H␈↓about␈α
the␈α
interrelationships␈α
between␈α
the␈α
numbers,␈α
but␈α
it␈α
does␈α
give␈α
us␈α
a␈α
notation␈α∞for␈α
representing
␈↓ ↓H␈↓them.␈α
 Thus␈α␈↓α2␈↓␈α
can␈α
be␈αused␈α
to␈αrepresent␈α
␈↓αtwo␈↓␈α
or␈αused␈α
as␈αan␈α
abbreviation␈α
for␈α␈↓↓S␈↓α(␈↓↓S␈↓α(0))␈↓.␈α
 One␈αbenefit␈α
of
␈↓ ↓H␈↓the␈α
␈↓↓S␈↓-notation␈α
is␈α
that␈α
it␈α
explicitly␈α
shows␈α
the␈α
means␈α
of␈α
construction.␈α
That␈α
is,␈α
it␈α
shows␈α
more␈α
of␈α
the
␈↓ ↓H␈↓properties␈α↔of␈α⊗the␈α↔these␈α⊗numbers␈α↔than␈α⊗just␈α↔distinguishability.␈α⊗ We␈α↔shall␈α⊗refer␈α↔to␈α↔the␈α⊗digit
␈↓ ↓H␈↓representation␈α
as␈α∞␈↓↓numerals␈↓␈α
and␈α
reserve␈α∞the␈α
term,␈α∞␈↓↓natural␈α
number␈↓,␈α
for␈α∞the␈α
abstract␈α∞object.␈α
 Thus
␈↓ ↓H␈↓numerals␈αdenote,␈αstand␈αfor,␈αor␈αrepresent␈αthe␈αabstract␈αobjects␈αcalled␈αnatural␈αnumbers;␈αand␈αdefinition
␈↓ ↓H␈↓␈↓	I␈↓ is better stated as: "a natural number can be represented as a finite sequence of digits".

␈↓ ↓H␈↓But␈αnotation␈αand␈αsyntax␈αare␈αnecessary␈αand␈αwe␈α
must␈αbe␈αable␈αto␈αgive␈αprecise␈αdescriptions␈αof␈α
syntactic
␈↓ ↓H␈↓notions.␈α
 Given␈α
a␈α∞choice␈α
between␈α
the␈α
two␈α∞previous␈α
definitions,␈α
␈↓	I␈↓␈α
and␈α∞␈↓	II␈↓,␈α
it␈α
appears␈α
that␈α∞␈↓	II␈↓␈α
is
␈↓ ↓H␈↓more␈α∂precise.␈α∂ Much␈α∂less␈α∂is␈α∂left␈α∂to␈α∂the␈α∞imagination;␈α∂given␈α∂␈↓αzero␈↓␈α∂and␈α∂a␈α∂definition␈α∂of␈α∂␈↓αsuccessor␈↓␈α∞the
␈↓ ↓H␈↓definition␈α∂will␈α∞act␈α∂as␈α∞a␈α∂recipe␈α∂for␈α∞producing␈α∂elements␈α∞of␈α∂␈↓N␈↓.␈α∂This␈α∞style␈α∂of␈α∞definition␈α∂is␈α∂called␈α∞an
␈↓ ↓H␈↓inductive definition.
␈↓ ↓H␈↓␈↓↓8  Symbolic expressions␈↓ 72.1␈↓



␈↓ ↓H␈↓The basic content of an inductive definition of a set of objects consists of three parts:

␈↓ ↓H␈↓␈↓ αh(1)␈αA␈αdescription␈αof␈αan␈αinitial␈αset␈αof␈αobjects;␈αthe␈αelements␈αof␈αthis␈αset␈αare␈αto␈αbe␈αincluded
␈↓ ↓H␈↓␈↓ αhautomatically in the set we are describing in the inductive definition.

␈↓ ↓H␈↓␈↓	IND␈↓

␈↓ ↓H␈↓␈↓ αh(2)␈αGiven␈αthe␈αdescription␈αof␈αsome␈αexisting␈αelements␈α
in␈αthe␈αset,␈αwe␈αare␈αgiven␈αa␈αmeans␈α
of
␈↓ ↓H␈↓␈↓ αhconstructing more elements.

␈↓ ↓H␈↓␈↓ αh(3)␈αAn␈αextremal␈αclause,␈αsaying␈αthat␈αthe␈αonly␈αelements␈αin␈αthe␈αset␈αare␈αthose␈αwhich␈αgained
␈↓ ↓H␈↓␈↓ αhadmittance by either (1) or (2).

␈↓ ↓H␈↓Clearly␈αour␈αdefinition␈αof␈α␈↓N␈↓␈α
in␈αterms␈αof␈α␈↓αzero␈↓␈αand␈α␈↓αsuccessor␈↓␈α
is␈αan␈αinstance␈αof␈α␈↓	IND␈↓:␈αwe␈α
are␈αdefining
␈↓ ↓H␈↓the␈αset␈αof␈αnatural␈αnumbers:␈α␈↓αzero␈↓␈αis␈αinitially␈αincluded␈αin␈αthe␈αset;␈αthen␈αapplying␈αthe␈αsecond␈αphrase␈αof
␈↓ ↓H␈↓the definition we can say that ␈↓αone␈↓ is in the set since ␈↓αone␈↓ is the successor of ␈↓αzero␈↓.

␈↓ ↓H␈↓We can recast the positional notation description as an inductive definition.

␈↓ ↓H␈↓␈↓ β(␈↓↓1.␈↓ A numeral is a digit

␈↓ ↓H␈↓␈↓ β(␈↓↓2.␈↓ if ␈↓αn␈↓ is a numeral then ␈↓αn␈↓ followed by a digit is a numeral.


␈↓ ↓H␈↓␈↓ β8␈↓↓3.␈↓ The only numerals are those created by finitely many applications of ␈↓↓1.␈↓ and ␈↓↓2.␈↓.

␈↓ ↓H␈↓In words, "a numeral is a digit, or a numeral followed by a digit".

␈↓ ↓H␈↓In␈α∂this␈α∂application␈α⊂of␈α∂␈↓	IND␈↓,␈α∂the␈α⊂initial␈α∂set␈α∂has␈α⊂more␈α∂than␈α∂one␈α⊂element;␈α∂namely␈α∂the␈α⊂ten␈α∂decimal
␈↓ ↓H␈↓digits.␈α
 Also␈α
we␈α
assume␈α
that␈α
the␈α
questioner␈αknows␈α
what␈α
"digit"␈α
means.␈α
 This␈α
is␈α
a␈α
characteristic␈αof␈α
all
␈↓ ↓H␈↓definitions:␈α
we␈α
must␈αstop␈α
␈↓↓somewhere␈↓␈α
in␈α
our␈αexplication.␈α
Notice␈α
too␈αthat␈α
we␈α
assume␈α
that␈α"followed
␈↓ ↓H␈↓by" means juxtaposition.

␈↓ ↓H␈↓Inductive␈α∞definitions␈α∞have␈α
been␈α∞the␈α∞province␈α
of␈α∞mathematics␈α∞for␈α
many␈α∞years;␈α∞however,␈α
computer
␈↓ ↓H␈↓science␈α∀has␈α∪encroached␈α∀a␈α∪bit␈α∀and␈α∪has␈α∀developed␈α∪a␈α∀style␈α∪of␈α∀syntax␈α∪specification␈α∀called␈α∪BNF
␈↓ ↓H␈↓(Backus-Naur␈αForm)␈αequations␈αwhich␈αhas␈αthe␈αsame␈αintent␈αas␈αthat␈αof␈αinductive␈αdefinitions.␈α Here␈αis
␈↓ ↓H␈↓the previous inductive definition of "numeral" as a set of BNF equations:

␈↓ ↓H␈↓<numeral>␈↓ β(::= <digit>
␈↓ ↓H␈↓<numeral>␈↓ β(::= <numeral><digit>

␈↓ ↓H␈↓As an abbreviation, the  two BNF equations may also be written:

␈↓ ↓H␈↓<numeral>␈↓ β(::= <digit>|<numeral><digit>.
␈↓ ↓H␈↓␈↓↓2.1␈↓ 	mIntroduction     9␈↓


␈↓ ↓H␈↓A␈α
comparison␈α
between␈αthe␈α
BNF␈α
and␈αthe␈α
inductive␈α
descriptions␈αof␈α
"numeral"␈α
should␈α
clarify␈αmuch
␈↓ ↓H␈↓of␈αthe␈α
notation,␈αbut␈αto␈α
be␈αcomplete␈αwe␈α
give␈αa␈α
more␈αdetailed␈αanalysis.␈α
 The␈αsymbol␈α"::="␈α
is␈αto␈αbe␈α
read
␈↓ ↓H␈↓"is␈αa",␈αthe␈αsymbol␈α"|"␈αis␈αto␈αbe␈αread␈α"or".␈α The␈αstrings␈αbeginning␈αwith␈α"<"␈αand␈αending␈αwith␈α
">"␈αstem
␈↓ ↓H␈↓from␈α⊃the␈α∩references␈α⊃to␈α⊃"numeral"␈α∩and␈α⊃"digit"␈α⊃in␈α∩␈↓↓1␈↓␈α⊃and␈α⊃␈↓↓2␈↓;␈α∩by␈α⊃convention,␈α⊃components␈α∩of␈α⊃BNF
␈↓ ↓H␈↓equations␈α⊃which␈α⊂␈↓↓describe␈↓␈α⊃elements␈α⊂are␈α⊃enclosed␈α⊂in␈α⊃"<"␈α⊂and␈α⊃">";␈α⊂and␈α⊃elements␈α⊂which␈α⊃are␈α⊂given
␈↓ ↓H␈↓␈↓↓explicitly␈↓␈α
are␈α
written␈α
without␈α
the␈α"<␈α
>"␈α
fence.␈α
 Thus␈α
"<digit>"␈α
is␈αnot␈α
a␈α
numeral␈α
but␈α
is␈αa␈α
description;
␈↓ ↓H␈↓to make the definition of <numeral> complete we should include an equation like:

␈↓ ↓H␈↓<digit>␈↓ β(:: = ␈↓α0 |1 |2 |3 |4 |5 |6 |7 |8 |9␈↓


␈↓ ↓H␈↓Juxtaposition␈αof␈αobjects␈αimplies␈α
concatenation␈αof␈αthe␈αsyntactic␈αobjects.␈α
Thus␈α"89"␈αis␈αan␈α
instance␈αof
␈↓ ↓H␈↓"<numeral><digit>".

␈↓ ↓H␈↓What␈α∂should␈α∂be␈α⊂remembered␈α∂from␈α∂the␈α⊂discussion␈α∂in␈α∂this␈α∂section?␈α⊂ First␈α∂we␈α∂wanted␈α⊂and␈α∂needed
␈↓ ↓H␈↓precise␈α∩ways␈α∪of␈α∩describing␈α∩the␈α∪elements␈α∩of␈α∩our␈α∪study␈α∩on␈α∩data␈α∪structures.␈α∩We␈α∩have␈α∪seen␈α∩that
␈↓ ↓H␈↓inductive␈α
definitions␈αare␈α
a␈α
powerful␈αway␈α
of␈α
describing␈αsets␈α
of␈αobjects.␈α
We␈α
have␈αseen␈α
a␈α
variant␈αof
␈↓ ↓H␈↓inductive␈α
definitions␈α
called␈αBackus-Naur␈α
Form␈α
equations.␈αWe␈α
will␈α
use␈αBNF␈α
equations␈α
to␈αdescribe
␈↓ ↓H␈↓the syntax of our data structures and our language.

␈↓ ↓H␈↓Second,␈α∂we␈α⊂have␈α∂begun␈α⊂to␈α∂see␈α⊂the␈α∂difference␈α∂between␈α⊂an␈α∂abstract␈α⊂object␈α∂and␈α⊂its␈α∂representation.
␈↓ ↓H␈↓This␈α∞distinction␈α∞has␈α∞been␈α∞well␈α∞studied␈α∞in␈α
philosophy␈α∞and␈α∞mathematics,␈α∞and␈α∞we␈α∞will␈α∞see␈α∞that␈α
this
␈↓ ↓H␈↓idea␈α∩has␈α∩strong␈α∩consequences␈α∩for␈α∩the␈α∩field␈α∩of␈α∩programming␈α∩and␈α∩computer␈α∩science␈α∩in␈α∩general.
␈↓ ↓H␈↓Representation of abstract objects will play a crucial role in this text.



␈↓ ↓H␈↓␈↓ βz␈↓↓2.2  Symbolic Expressions: abstract data structures␈↓


␈↓ ↓H␈↓We␈αnow␈αwish␈αto␈αapply␈αthe␈αtechniques␈αand␈αideas␈αwhich␈αwe␈αhave␈αdeveloped␈αin␈αthe␈αprevious␈αsection.
␈↓ ↓H␈↓We␈α∂wish␈α⊂to␈α∂show␈α∂that␈α⊂careful␈α∂definitions␈α⊂and␈α∂use␈α∂of␈α⊂abstraction␈α∂will␈α∂benefit␈α⊂the␈α∂study␈α⊂of␈α∂data
␈↓ ↓H␈↓structures␈α∞and␈α∞LISP.␈α∞ To␈α∂begin␈α∞our␈α∞study␈α∞we␈α∞should␈α∂therefore␈α∞characterize␈α∞the␈α∞domain␈α∂of␈α∞LISP
␈↓ ↓H␈↓data structures in a manner similar to what we did for numbers.

␈↓ ↓H␈↓Our␈α∂objects␈α⊂are␈α∂called␈α⊂␈↓↓Symbolic␈α∂Expressions␈↓.␈α∂ Our␈α⊂domain␈α∂of␈α⊂Symbolic␈α∂Expressions␈α⊂is␈α∂named
␈↓ ↓H␈↓␈↓<sexpr>␈↓.  Symbolic expressions are also known as ␈↓¬S-expressions␈↓ or ␈↓¬S-exprs␈↓.

␈↓ ↓H␈↓The␈α∂set␈α∂of␈α∞symbolic␈α∂expressions␈α∂is␈α∞defined␈α∂inductively␈α∂over␈α∞a␈α∂base␈α∂set␈α∞named␈α∂␈↓<atom>␈↓.␈α∂The␈α∞set
␈↓ ↓H␈↓␈↓<atom>␈↓␈α∂can␈α∞itself␈α∂be␈α∞defined␈α∂inductively.␈α∞We␈α∂give␈α∞the␈α∂BNF␈α∞equations␈α∂for␈α∞elements␈α∂of␈α∞␈↓<atom>␈↓
␈↓ ↓H␈↓below,␈αbut␈αthe␈αessential␈αcharacter␈αof␈αthe␈αdomain␈αis␈αthat␈αit␈αcontains␈αtwo␈αkinds␈αof␈αobjects:␈αthe␈α␈↓↓literal
␈↓ ↓H␈↓↓atoms␈↓ and the signed numerals.  The elements of ␈↓<atom>␈↓ are called ␈↓↓atoms␈↓.
␈↓ ↓H␈↓␈↓↓10  Symbolic expressions␈↓ 52.2␈↓


␈↓ ↓H␈↓<atom>␈↓ β(:: = <literal atom>|<numeral>| -<numeral>
␈↓ ↓H␈↓<literal atom>␈↓ β(:: = <atom letter>|<literal atom><atom letter>|<literal atom><digit>
␈↓ ↓H␈↓<numeral>␈↓ β(:: = <digit>|<numeral><digit>
␈↓ ↓H␈↓<atom letter>␈↓ β(:: =␈↓α A |B |C ...| Z␈↓ ␈↓π 2␈↓
␈↓ ↓H␈↓<digit>␈↓ β(:: = ␈↓α0 |1 |2 ... |9␈↓

␈↓ ↓H␈↓Thus␈α
a␈αliteral␈α
atom␈αis␈α
a␈αstring␈α
of␈αuppercase␈α
letters␈α
and␈αdigits,␈α
subject␈αto␈α
the␈αprovision␈α
that␈αthe␈α
␈↓↓first␈↓
␈↓ ↓H␈↓character in the atom be a letter.


␈↓ ↓H␈↓For example:␈↓ βxatoms␈↓ ¬hnot atoms
␈↓ ↓H␈↓␈↓α␈↓ βxABC123␈↓ ¬h2a
␈↓ ↓H␈↓α␈↓ βx12␈↓ ¬ha
␈↓ ↓H␈↓α␈↓ βxA4D6␈↓ ¬h$$g
␈↓ ↓H␈↓α␈↓ βxNIL␈↓ ¬hABD.
␈↓ ↓H␈↓α␈↓ βxT␈↓ ¬h(A . B)


␈↓ ↓H␈↓The␈α∞characteristics␈α∞of␈α∞atoms␈α
which␈α∞most␈α∞interest␈α∞us␈α∞are␈α
their␈α∞distinguishability:␈α∞the␈α∞atom␈α∞␈↓αABC␈↓␈α
is
␈↓ ↓H␈↓distinguishable␈α
from␈α
the␈α
atom␈α
␈↓αAB␈↓.␈α
That␈α
the␈α
string␈α
␈↓αAB␈↓␈αis␈α
a␈α
part␈α
of␈α
the␈α
string␈α
␈↓αABC␈↓␈α
is␈α
not␈αgermane␈α
to
␈↓ ↓H␈↓our␈α⊗current␈α⊗discussion␈↓π 3␈↓.␈α⊗ Similarly,␈α⊗we␈α⊗will␈α⊗seldom␈α⊗need␈α⊗to␈α⊗exploit␈α⊗numerical␈α∃relationships
␈↓ ↓H␈↓underlying␈α∩the␈α∩numerals.␈α∩At␈α∪best␈α∩we␈α∩will␈α∩use␈α∩simple␈α∪counting␈α∩properties.␈α∩ Thus␈α∩most␈α∪of␈α∩our
␈↓ ↓H␈↓discussions␈α
will␈α
deal␈α
with␈α
non-numeric␈α
atoms.␈α
 Most␈α
implementations␈α
of␈α
LISP␈α
do␈α
however␈α
contain␈α
a
␈↓ ↓H␈↓large␈αarithmetic␈αentourage.␈α Many␈αimplementations␈αalso␈α
give␈αa␈αwider␈αclass␈αof␈αliteral␈αatoms,␈α
allowing
␈↓ ↓H␈↓some special characters to appear; for most of our discussion the above class is quite sufficient.

␈↓ ↓H␈↓The␈α⊃domain␈α⊃of␈α⊃Symbolic␈α⊃expressions,␈α⊃called␈α⊂␈↓<sexpr>␈↓␈α⊃is␈α⊃defined␈α⊃inductively␈α⊃over␈α⊃the␈α⊂domain
␈↓ ↓H␈↓␈↓<atom>␈↓␈↓π 4␈↓.

␈↓ ↓H␈↓␈↓ β8␈↓↓1.␈↓ Any element of ␈↓<atom>␈↓ is an element of ␈↓<sexpr>␈↓.

␈↓ ↓H␈↓␈↓ β8␈↓↓2.␈↓␈α∂If␈α⊂␈↓λα␈↓β1␈↓␈α∂and␈α⊂␈↓λα␈↓β2␈↓␈α∂are␈α⊂elements␈α∂of␈α∂␈↓<sexpr>␈↓,␈α⊂then␈α∂the␈α⊂␈↓↓dotted-pair␈↓␈α∂␈↓α(␈↓λα␈↓β1␈↓α.␈↓λα␈↓β2␈↓α)␈↓␈α⊂is␈α∂in
␈↓ ↓H␈↓␈↓ β8␈↓<sexpr>␈↓.

␈↓ ↓H␈↓Thus␈α␈↓<sexpr>␈↓␈αincludes␈α␈↓<atom>␈↓␈αas␈αa␈αproper␈αsubset.␈αThe␈αnotation␈αwe␈αchose␈αfor␈αthe␈αdotted-pairs␈αis
␈↓ ↓H␈↓the following:

␈↓ ↓H␈↓A␈α
dotted-pair␈αconsists␈α
of␈α
a␈αleft-parenthesis␈α
followed␈α
by␈αan␈α
S-expr,␈α
followed␈αby␈α
a␈α
period,␈αfollowed␈α
by
␈↓ ↓H␈↓␈↓ αhan S-expr, followed by a right-parenthesis.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 2␈↓ We use ellipses here as a convienient abbreviation.

␈↓ ↓H␈↓␈↓π 3␈↓ However, we will discuss such topics in Section  on string processing.

␈↓ ↓H␈↓␈↓π 4␈↓ We will not give the extremal clause, but it is assumed to hold.
␈↓ ↓H␈↓␈↓↓2.2␈↓ ε∀Symbolic Expressions: abstract data structures     11␈↓


␈↓ ↓H␈↓In␈α
the␈α∞definition␈α
of␈α∞␈↓<sexpr>␈↓␈α
we␈α∞have␈α
introduced␈α∞␈↓λα␈↓βi␈↓␈α
as␈α∞␈↓↓match-variables␈↓.␈α
 Greek␈α∞letters␈α
␈↓λα␈↓␈α∞and␈α
␈↓λβ␈↓
␈↓ ↓H␈↓will␈α
be␈α
used␈αthroughout␈α
the␈α
text␈αin␈α
several␈α
contexts␈αto␈α
designate␈α
pattern␈αmatches.␈α
For␈α
example,␈αif
␈↓ ↓H␈↓we␈α
let␈α
␈↓λα␈↓β1␈↓␈α∞be␈α
␈↓α12␈↓␈α
and␈α
let␈α∞␈↓λα␈↓β2␈↓␈α
be␈α
␈↓αABC␈↓␈α
then␈α∞␈↓α(␈↓λα␈↓β1␈↓α.␈↓λα␈↓β2␈↓α)␈↓␈α
is␈α
␈↓α(12 . ABC)␈↓␈α
or␈α∞if␈α
we␈α
let␈α
␈↓α(A .(B . C))␈↓␈α∞be␈α
␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓
␈↓ ↓H␈↓then ␈↓λα␈↓ is ␈↓αA␈↓ and ␈↓λβ␈↓ is ␈↓α(B . C)␈↓.

␈↓ ↓H␈↓Finally here's a BNF description of the full set of S-expressions.

␈↓ ↓H␈↓␈↓ ∧O<sexpr> :: = <atom>|␈↓α(␈↓<sexpr> . <sexpr>␈↓α) 

␈↓ ↓H␈↓Notice␈α∂that␈α∂if␈α∂we␈α∂allow␈α∂floating␈α∂point␈α∂numbers␈α∂as␈α∂atoms␈α∂some␈α∂care␈α∂needs␈α∂to␈α∂be␈α∂exercised␈α∞when
␈↓ ↓H␈↓writing␈αS-expressions.␈α
How␈αshould␈α
␈↓α(A.1.2)␈↓␈αbe␈α
interpreted?␈α Is␈α
it␈αthe␈α
dotted␈αpair␈α
␈↓α(A␈α.␈α
1.2)␈↓,␈αor␈α
is␈αit␈α
just
␈↓ ↓H␈↓an␈α→ill-formed␈α~expression?␈α→ Evaluation␈α→of␈α~such␈α→ambiguous␈α→constructs␈α~will␈α→depend␈α~on␈α→the
␈↓ ↓H␈↓implementation; such details are discussed later.

␈↓ ↓H␈↓Examples:␈↓ βxS-exprs␈↓ ε8not S-exprs
␈↓ ↓H␈↓α␈↓ βxA␈↓ ε8A . B
␈↓ ↓H␈↓α␈↓ βx(A . B)␈↓ ε8(A . B . C)
␈↓ ↓H␈↓α␈↓ βx(((A.B) .C) . (A.B))␈↓ ε8((A . B)))

␈↓ ↓H␈↓Recall␈αour␈α
caveat␈αon␈α
numerals␈αand␈αnumbers.␈α
It␈αalso␈α
applies␈αhere.␈αWhen␈α
we␈αdescribed␈α
the␈αdomain
␈↓ ↓H␈↓␈↓<sexpr>␈↓␈α∂we␈α∞picked␈α∂a␈α∞␈↓↓specific␈↓␈α∂syntactic␈α∂representation␈α∞for␈α∂its␈α∞elements.␈α∂ It␈α∞will␈α∂be␈α∂a␈α∞convenient
␈↓ ↓H␈↓notation␈αsince␈α
it␈αmakes␈αexplicit␈α
the␈αconstruction␈αof␈α
the␈αcomposite␈αS-expr␈α
from␈αits␈αcomponents␈↓π 5␈↓,␈α
and
␈↓ ↓H␈↓the notation is also consistent with LISP history.

␈↓ ↓H␈↓However␈α⊂there␈α⊂is␈α⊂more␈α⊂to␈α⊂the␈α⊂domain␈α⊂␈↓<sexpr>␈↓␈α⊂than␈α⊂syntax,␈α⊂just␈α⊂as␈α⊂there␈α⊂is␈α⊂more␈α⊂to␈α⊂␈↓N␈↓␈α⊂than
␈↓ ↓H␈↓positional␈α∞notation␈↓π 6␈↓.␈α∞ What␈α∂␈↓↓are␈↓␈α∞the␈α∞essential␈α∞features␈α∂of␈α∞S-expressions?␈α∞Symbolic␈α∂expressions␈α∞are
␈↓ ↓H␈↓either␈α
atomic␈α
or␈α
they␈α
have␈α
two␈α∞components.␈α
If␈α
we␈α
are␈α
confronted␈α
with␈α
a␈α∞non-atomic␈α
S-expression
␈↓ ↓H␈↓then␈αwe␈αwant␈αa␈αmeans␈αof␈αdistinguishing␈αbetween␈αthe␈α"first"␈αand␈αthe␈α"second"␈αcomponent.␈αThe␈α"dot
␈↓ ↓H␈↓notation"␈αdoes␈α
this␈αfor␈α
us,␈αbut␈α
obviously␈α"(",␈α
")",␈αand␈α
"."␈αof␈α
the␈αdotted-pairs␈α
are␈αsimply␈α
notation␈αor
␈↓ ↓H␈↓syntax.␈α∂ We␈α∂could␈α∞have␈α∂just␈α∂as␈α∞well␈α∂represented␈α∂the␈α∞dotted-pair␈α∂of␈α∂␈↓αA␈↓␈α∞and␈α∂␈↓αB␈↓␈α∂as␈α∂the␈α∞set-theoretic
␈↓ ↓H␈↓ordered pair, ␈↓α<A,B>␈↓ or any other notation which preserves the essentials of the domain ␈↓<sexpr>␈↓.

␈↓ ↓H␈↓The␈α⊃distinctions␈α⊃between␈α∩abstract␈α⊃objects␈α⊃and␈α⊃their␈α∩representation␈α⊃are␈α⊃quite␈α⊃important.␈α∩As␈α⊃we
␈↓ ↓H␈↓continue␈αour␈αstudy␈αof␈αmore␈αand␈αmore␈αcomplex␈αdata␈αstructures␈αthe␈αuse␈αof␈αan␈αabstract␈αdata␈αstructure
␈↓ ↓H␈↓instead␈α∞of␈α
one␈α∞of␈α
its␈α∞representations␈α
can␈α∞mean␈α
the␈α∞difference␈α
between␈α∞a␈α
clear␈α∞and␈α∞clean␈α
program
␈↓ ↓H␈↓and␈α∀a␈α∀confusing␈α∀and␈α∀complicated␈α∀program.␈α∀There␈α∀are␈α∀similar␈α∀gains␈α∀for␈α∀us␈α∀when␈α∃we␈α∀study
␈↓ ↓H␈↓algorithms␈α⊂defined␈α∂over␈α⊂these␈α∂abstract␈α⊂data␈α∂structures.␈α⊂The␈α∂less␈α⊂the␈α∂algorithm␈α⊂knows␈α⊂about␈α∂the

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 5␈↓␈α∂Just␈α∂as␈α∂the␈α∞"successor"␈α∂notation␈α∂shows␈α∂the␈α∞construction␈α∂of␈α∂the␈α∂numbers␈α∞from␈α∂␈↓α0␈↓.␈α∂This␈α∂kind␈α∞of
␈↓ ↓H␈↓notation␈α
will␈αbe␈α
much␈αmore␈α
useful␈α
in␈αLISP,␈α
since␈αour␈α
interest␈α
in␈αdata␈α
structures␈αwill␈α
focus␈α
on␈αthe
␈↓ ↓H␈↓construction process and the interrelationships between components of an S-expr.

␈↓ ↓H␈↓␈↓π 6␈↓␈α␈↓α2␈↓,␈α
II␈αin␈αRoman␈α
numerals,␈α10␈αin␈α
binary,␈α"zwei"␈αin␈α
German␈α...␈αare␈α
all␈αrepresentations␈αof␈α
the␈αsame
␈↓ ↓H␈↓number.
␈↓ ↓H␈↓␈↓↓12  Symbolic expressions␈↓ 52.2␈↓


␈↓ ↓H␈↓representation␈α
of␈α
the␈α∞data␈α
structure,␈α
the␈α
easier␈α∞it␈α
will␈α
be␈α
to␈α∞modify␈α
or␈α
understand␈α∞that␈α
algorithm.
␈↓ ↓H␈↓Indeed␈αyou␈αmay␈αhave␈αalready␈αexperienced␈αthis␈αphenomenon␈αif␈αyou␈αhave␈αprogrammed.␈α A␈αprogram
␈↓ ↓H␈↓written␈α
in␈α
a␈α
high-level␈α
language␈α
is␈αalmost␈α
always␈α
more␈α
understandable␈α
than␈α
its␈αmachine-language
␈↓ ↓H␈↓counterpart.␈α⊃ The␈α⊃high-level␈α⊃program␈α⊃is␈α⊃more␈α⊃abstract␈α⊃whereas␈α⊃the␈α∩machine-language␈α⊃program
␈↓ ↓H␈↓knows␈α∞a␈α∞great␈α∞deal␈α∞about␈α∞representations.␈α∞ Finally,␈α
if␈α∞you␈α∞still␈α∞doubt␈α∞that␈α∞representations␈α∞make␈α
a
␈↓ ↓H␈↓difference␈α
in␈α∞clarity,␈α
try␈α
doing␈α∞long␈α
division␈α
in␈α∞Roman␈α
numerals.␈α
 We␈α∞will␈α
say␈α
much␈α∞more␈α
about
␈↓ ↓H␈↓abstraction and representation in algorithms and data structures as we proceed.



␈↓ ↓H␈↓␈↓ βy␈↓↓2.3  Trees: representations of Symbolic expressions␈↓


␈↓ ↓H␈↓Besides␈α_the␈α_more␈α_conventional␈α_typographical␈α_notations,␈α_S-expressions␈α_also␈α_have␈α↔interesting
␈↓ ↓H␈↓␈↓↓graphical␈↓␈α∂representations.␈α∂ S-exprs␈α∂have␈α∂a␈α∂natural␈α∂interpretation␈α∂as␈α∂a␈α∂structure␈α∂which␈α∂we␈α∂call␈α∞a
␈↓ ↓H␈↓LISP-tree or L-tree.


␈↓ ↓H␈↓Here are some L-trees:
␈↓"␈↓ ↓H␈↓∂             /\                             /\
␈↓"␈↓ ↓H␈↓∂          L /  \ R                         /  \R
␈↓"␈↓ ↓H␈↓∂           /    \                        L/   /\
␈↓"␈↓ ↓H␈↓∂        L /\R  L/\R                      /  L/  \R
␈↓"␈↓ ↓H␈↓∂         /  \  /  \                     ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αNIL␈↓∂
␈↓"␈↓ ↓H␈↓∂        ␈↓α1␈↓∂   ␈↓α2␈↓∂  ␈↓αA␈↓∂ L/\R
␈↓"␈↓ ↓H␈↓∂                 ␈↓αD␈↓∂  ␈↓αE␈↓∂

␈↓ ↓H␈↓We can give an inductive definition:

␈↓ ↓H␈↓␈↓ β8␈↓↓1.␈↓␈α
Any␈αlabelled␈α
terminal␈αnode␈α
is␈α
a␈αL-tree.␈α
A␈αlabelled␈α
terminal␈α
node␈αis␈α
a␈αsymbol,␈α
␈↓λ.␈↓,
␈↓ ↓H␈↓␈↓ β8with an element of ␈↓<atom>␈↓ as a subscript.  For example ␈↓λ.␈↓βABC␈↓.

␈↓ ↓H␈↓␈↓ β8␈↓↓2.␈↓␈αIf␈α␈↓αn␈↓β1␈↓␈αand␈α␈↓αn␈↓β2␈↓␈αare␈αL-trees␈αthen␈αattaching␈αthe␈αtails␈αof␈αthe␈αarrows,␈α␈↓λL␈↓␈αand␈α␈↓λR␈↓␈αto␈α␈↓λ.␈↓
␈↓ ↓H␈↓␈↓ β8and the heads of the arrows to ␈↓αn␈↓β1␈↓ and ␈↓αn␈↓β2␈↓ also forms an L-tree.

␈↓ ↓H␈↓Most␈α
important:␈αthere␈α
are␈α
no␈αintersecting␈α
branches.␈α
Later␈αwe␈α
will␈α
talk␈αabout␈α
more␈αgeneral␈α
structures
␈↓ ↓H␈↓called list-structures.

␈↓ ↓H␈↓You␈αcan␈αsee␈αhow␈αto␈αinterpret␈αS-exprs␈αas␈αL-trees.␈α The␈αatoms␈αare␈αinterpreted␈αas␈αterminal␈αnodes;␈αand
␈↓ ↓H␈↓since␈α
non-atomic␈αS-exprs␈α
always␈α
have␈αtwo␈α
sub-expressions␈αwe␈α
can␈α
write␈αthe␈α
first␈α
subexpression␈αas
␈↓ ↓H␈↓the␈α∞left␈α∂branch␈α∞of␈α∂an␈α∞L-tree␈α∂and␈α∞the␈α∞second␈α∂sub-␈α∞expression␈α∂as␈α∞the␈α∂right␈α∞branch.␈α∂ Typically␈α∞we,
␈↓ ↓H␈↓leave␈α∂off␈α∂the␈α∂␈↓βL␈↓␈α∂(left)␈α∂and␈α∞␈↓βR␈↓␈α∂(right)␈α∂subscripts␈α∂since␈α∂it␈α∂is␈α∞clear␈α∂from␈α∂context␈α∂which␈α∂they␈α∂are.␈α∞ For
␈↓ ↓H␈↓example:
␈↓ ↓H␈↓%22.3␈↓ ε∃Trees: representations of Symbolic expressions     13%*


␈↓"␈↓ ↓H␈↓∂     ␈↓α(A . B)␈↓∂␈↓ ¬_␈↓α(A . (B . C))␈↓∂␈↓ λ(␈↓α((A . B) . C)␈↓∂

␈↓"␈↓ ↓H␈↓∂       /\␈↓ ¬_     /\␈↓ λ(     /\
␈↓"␈↓ ↓H␈↓∂      /  \␈↓ ¬_    /  \␈↓ λ(    /  \
␈↓"␈↓ ↓H␈↓∂     ␈↓αA␈↓∂   ␈↓αB␈↓∂␈↓ ¬_   /   /\␈↓ λ(   /\   \
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_  /   /  \␈↓ λ(  /  \   \
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_ ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αC␈↓∂␈↓ λ( ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αC␈↓∂

␈↓ ↓H␈↓Other␈α_representations␈α→of␈α_LISP-trees␈α_which␈α→will␈α_be␈α_more␈α→suggestive␈α_when␈α_we␈α→talk␈α_about
␈↓ ↓H␈↓implementation of LISP are: ␈↓α
␈↓"␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
                  ααααα→~ # ~ #αβααααααααααα→~ # ~ # ~
␈↓"␈↓ ↓H␈↓
                        %αβα∀ααα$            %αβα∀αβα$
␈↓"␈↓ ↓H␈↓
                          ↓                    ↓   ↓
␈↓"␈↓ ↓H␈↓
                          ␈↓αA␈↓
                    ␈↓αB␈↓
   ␈↓αC␈↓



␈↓ ↓H␈↓α␈↓or equivalently:
␈↓"␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
                  ααααα→~ A ~ #αβααααααααααα→~ B ~ C ~
␈↓"␈↓ ↓H␈↓
                        %ααα∀ααα$            %ααα∀ααα$

␈↓ ↓H␈↓This representation (for the S-expr ␈↓α(A .(B . C))␈↓ ) is called ␈↓↓box-notation␈↓.

␈↓ ↓H␈↓Again␈α∞please␈α∂keep␈α∞in␈α∞mind␈α∂the␈α∞distinction␈α∞between␈α∂the␈α∞abstract␈α∞object␈α∂referred␈α∞to␈α∞as␈α∂a␈α∞symbolic
␈↓ ↓H␈↓expression and the several representations which we have shown.

␈↓ ↓H␈↓The␈α⊂question␈α⊂of␈α⊂representation␈α⊃is␈α⊂so␈α⊂important␈α⊂and␈α⊂will␈α⊃occur␈α⊂so␈α⊂frequently␈α⊂that␈α⊃we␈α⊂introduce
␈↓ ↓H␈↓notation␈αfor␈αa␈αrepresentational␈αmapping,␈α␈↓λr␈↓.␈α To␈αrepresent␈αdomain␈α␈↓D␈↓␈αin␈αdomain␈α␈↓E␈↓,␈αwe␈αwill␈αdefine␈α
a
␈↓ ↓H␈↓function ␈↓λr␈↓βD→E␈↓ which usually will be inductively given, and will express the desired mapping.

␈↓ ↓H␈↓For example a representational mapping ␈↓λr␈↓β<sexpr>→L-tree␈↓ can be given:
␈↓ ↓H␈↓␈↓ ¬@␈↓λr␈↓∞(␈↓<atom>␈↓∞)␈↓ = ␈↓λ.␈↓β<atom>␈↓
␈↓ ↓H␈↓␈↓ ¬∞and for ␈↓λα␈↓ and ␈↓λβ␈↓ in ␈↓<sexpr>␈↓:
␈↓"␈↓ ↓H␈↓∂␈↓ ∧H␈↓λr␈↓∞(␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓∞)␈↓ =␈↓∂␈↓ ελ     /\
␈↓"␈↓ ↓H␈↓∂␈↓ ∧H␈↓ ελ    /  \
␈↓"␈↓ ↓H␈↓∂␈↓ ∧H␈↓ ελ   /    \
␈↓"␈↓ ↓H␈↓∂␈↓ ∧H␈↓ ελ␈↓λr␈↓∞(␈↓λα␈↓∞)␈↓∂   ␈↓λr␈↓∞(␈↓λβ␈↓∞)␈↓∂

␈↓ ↓H␈↓Typically context will determine the appropriate subscript on the ␈↓λr␈↓-mapping; thus we will omit it.
␈↓ ↓H␈↓␈↓↓14  Symbolic expressions␈↓ 52.3␈↓


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓I Which of the following are dotted-pairs?
␈↓ ↓H␈↓␈↓ αX␈↓↓1.␈↓α (X . Y)   ␈↓↓2.␈↓α ((A .(B . C))  ␈↓↓3.␈↓α  A2   ␈↓↓4.␈↓α (X . Y2 . Z)


␈↓ ↓H␈↓II Write the following as LISP trees:
␈↓ ↓H␈↓␈↓ αX␈↓↓1.␈↓α  ((A . B).(B . (C . D)))  ␈↓↓2.␈↓α  (((A . B).C).E)

␈↓ ↓H␈↓α␈↓ αX␈↓↓3.␈↓α  ((X . NIL).(Y .(Z . NIL)))    ␈↓↓4.␈↓α  (NIL . NIL)


␈↓ ↓H␈↓III Write the following LISP trees as S-exprs:


␈↓"␈↓ ↓H␈↓∂          ␈↓↓1.␈↓∂␈↓ βx ␈↓↓2.␈↓∂␈↓ ε8 ␈↓↓3.␈↓∂
␈↓"␈↓ ↓H␈↓∂            /\␈↓ βx   /\␈↓ ε8    /\
␈↓"␈↓ ↓H␈↓∂           /  \␈↓ βx  /  \␈↓ ε8   /  \
␈↓"␈↓ ↓H␈↓∂          /   /\␈↓ βx ␈↓αA␈↓∂   /\␈↓ ε8  ␈↓αA␈↓∂   /\
␈↓"␈↓ ↓H␈↓∂         /   /  \␈↓ βx    /  \␈↓ ε8     /  \
␈↓"␈↓ ↓H␈↓∂        ␈↓αA␈↓∂   ␈↓αB␈↓∂    ␈↓αC␈↓∂␈↓ βx   /    \␈↓ ε8    ␈↓αB␈↓∂   /\
␈↓"␈↓ ↓H␈↓∂␈↓ βx  /\    /\␈↓ ε8       /  \
␈↓"␈↓ ↓H␈↓∂␈↓ βx /  \   ␈↓αD␈↓∂ ␈↓αE␈↓∂␈↓ ε8      ␈↓αC␈↓∂   /\
␈↓"␈↓ ↓H␈↓∂␈↓ βx ␈↓αA␈↓∂  ␈↓αNIL␈↓∂␈↓ ε8         /  \
␈↓"␈↓ ↓H␈↓∂␈↓ βx␈↓ ε8        ␈↓αD␈↓∂   ␈↓αNIL␈↓∂




␈↓"␈↓ ↓H␈↓
␈↓4.␈↓
                         ␈↓5.␈↓

␈↓"␈↓ ↓H␈↓
⊂αααααπααα⊃  ⊂αααπααααα⊃        ⊂ααααααπααα⊃  ⊂αααπααα⊃  ⊂αααπααααα⊃
␈↓"␈↓ ↓H␈↓
~  #  ~ #αβα→~ # ~  #  ~        ~ CONS ~ #αβα→~ X ~ #αβα→~ Y ~ NIL ~
␈↓"␈↓ ↓H␈↓
%ααβαα∀ααα$  %αβα∀ααβαα$        %αααααα∀ααα$  %ααα∀ααα$  %ααα∀ααααα$
␈↓"␈↓ ↓H␈↓
   ↓           ~    ↓
␈↓"␈↓ ↓H␈↓
  CAR          ~   NIL
␈↓"␈↓ ↓H␈↓
               ~  ⊂αααααπααα⊃  ⊂αααπααααα⊃
␈↓"␈↓ ↓H␈↓
               %α→~  #  ~ #αβα→~ # ~  #  ~
␈↓"␈↓ ↓H␈↓
                  %ααβαα∀ααα$  %αβα∀ααβαα$
␈↓"␈↓ ↓H␈↓
                     ↓           ↓    ↓
␈↓"␈↓ ↓H␈↓
                   QUOTE         A   NIL
␈↓ ↓H␈↓␈↓↓2.4␈↓ λyPrimitive Functions     15␈↓


␈↓ ↓H␈↓␈↓ ¬+␈↓↓2.4  Primitive Functions␈↓


␈↓ ↓H␈↓So␈α∂far␈α∞we␈α∂have␈α∞described␈α∂the␈α∞domain␈α∂of␈α∞abstract␈α∂objects␈α∞called␈α∂Symbolic␈α∞Expressions␈α∂and␈α∞have
␈↓ ↓H␈↓exhibited␈α∪several␈α∪representations␈α∀for␈α∪these␈α∪objects.␈α∪ We␈α∀will␈α∪now␈α∪describe␈α∪some␈α∀functions␈α∪or
␈↓ ↓H␈↓operations␈αto␈αbe␈αperformed␈αon␈αthis␈αdomain.␈α We␈αneed␈α
to␈αbe␈αa␈αbit␈αcareful␈αhere.␈αWe␈αare␈αabout␈αto␈α
see
␈↓ ↓H␈↓one␈α∞of␈α∞the␈α∞main␈α∞differences␈α∞between␈α∞mathematics␈α∞and␈α∞computer␈α∞science:␈α∂mathematics␈α∞emphasizes
␈↓ ↓H␈↓the idea of function; computer science emphasizes the idea of algorithm, process, or procedure.

␈↓ ↓H␈↓What␈α⊃is␈α⊃a␈α⊃function?␈α⊃Mathematically␈α⊃a␈α⊃function␈α⊃is␈α⊃simply␈α⊃a␈α⊃mapping␈α⊃such␈α⊃that␈α⊃for␈α⊃any␈α⊂given
␈↓ ↓H␈↓argument␈αin␈α
the␈αdomain␈α
of␈αthe␈α
function␈αthere␈αexists␈α
a␈αunique␈α
corresponding␈αvalue.␈α
In␈αelementary
␈↓ ↓H␈↓set␈α_theory,␈α_a␈α_definition␈α_of␈α_function␈α_␈↓αf␈↓␈α_involves␈α↔saying␈α_that␈α_␈↓αf␈↓␈α_is␈α_a␈α_set␈α_of␈α_ordered␈α↔pairs
␈↓ ↓H␈↓␈↓αf = { <x␈↓β1␈↓α, y␈↓β1␈↓α>, ...}␈↓;␈α∂the␈α∂␈↓αx␈↓βi␈↓'s␈α∂are␈α∂all␈α∂distinct␈α∂and␈α∂the␈α∂value␈α∂of␈α∂the␈α∂function␈α∂␈↓αf␈↓␈α∂for␈α∂an␈α∂argument␈α∂␈↓αx␈↓βi␈↓␈α∂is
␈↓ ↓H␈↓defined␈αto␈αbe␈αthe␈αcorresponding␈α␈↓αy␈↓βi␈↓.␈α With␈αeither␈αdefinition␈αno␈αrule␈αof␈αcomputation␈αis␈αgiven␈αto␈αhelp
␈↓ ↓H␈↓locate␈α⊂values;␈α⊂with␈α⊃the␈α⊂first␈α⊂definition␈α⊂it␈α⊃is␈α⊂implicit␈α⊂that␈α⊂the␈α⊃internal␈α⊂structure␈α⊂of␈α⊃the␈α⊂mapping
␈↓ ↓H␈↓doesn't matter; in the set-theoretic definition, the correspondence is explicitly given.

␈↓ ↓H␈↓An␈α
algorithm␈αor␈α
procedure␈αis␈α
a␈α
process␈αfor␈α
computing␈αvalues␈α
for␈α
a␈αfunction.␈α
The␈αfactorial␈α
function,
␈↓ ↓H␈↓␈↓αn!␈↓, can be computed by many different algorithms; but thought of as a function it is a set

␈↓ ↓H␈↓␈↓ ∧\␈↓α{<0,1>, <1,1>, <2,2>, <3,6>, ...<n,n!>, ...}␈↓ 

␈↓ ↓H␈↓For␈α∞the␈α
initial␈α∞rash␈α
of␈α∞primitives,␈α
the␈α∞distinction␈α
between␈α∞function␈α
and␈α∞algorithm␈α
will␈α∞not␈α∞be␈α
too
␈↓ ↓H␈↓apparent. However we will soon remedy that situation.

␈↓ ↓H␈↓Now␈αfor␈αsome␈αterminology:␈αthe␈α␈↓↓domain␈↓␈α
of␈αa␈αfunction␈αis␈αthe␈αset␈α
of␈αall␈αvalues␈αfor␈αwhich␈αthe␈α
function
␈↓ ↓H␈↓is␈αdefined;␈αthe␈α␈↓↓range␈↓␈αof␈αa␈αfunction␈αis␈αthe␈α
set␈αof␈αall␈αvalues␈αwhich␈αthe␈αfunction␈αtakes␈αon.␈α
 A␈αcareful
␈↓ ↓H␈↓definition␈α
of␈α
a␈α∞function,␈α
␈↓αf␈↓,␈α
requires␈α
specification␈α∞of␈α
a␈α
further␈α
set␈α∞called␈α
the␈α
␈↓↓domain␈α∞of␈α
discourse␈↓.
␈↓ ↓H␈↓The␈α
domain␈α
of␈α
discourse,␈α
named␈α
␈↓	D␈↓,␈α
consists␈α
of␈α
all␈α
possible␈α
values␈α
which␈α
may␈α
occur␈α
as␈α
the␈α
argument
␈↓ ↓H␈↓to␈αa␈α
function.␈α If␈αthe␈α
domain␈αof␈α
a␈αparticular␈αfunction␈α
␈↓αf␈↓␈αcoincides␈αwith␈α
␈↓	D␈↓␈αthen␈α
␈↓αf␈↓␈αis␈αsaid␈α
to␈αbe␈αa␈α
␈↓↓total
␈↓ ↓H␈↓↓function␈↓␈α(over␈α␈↓	D␈↓);␈αif␈αthere␈αare␈αelements␈αof␈α␈↓	D␈↓␈αwhich␈αare␈αnot␈αin␈αthe␈αdomain␈αof␈α␈↓αf␈↓␈αthen␈α␈↓αf␈↓␈αis␈αa␈αpartial
␈↓ ↓H␈↓function,␈α∂and␈α∞␈↓αf␈↓␈α∂is␈α∞said␈α∂to␈α∞be␈α∂undefined␈α∂for␈α∞those␈α∂values.␈α∞ For␈α∂example,␈α∞the␈α∂factorial␈α∂function␈α∞is
␈↓ ↓H␈↓typically␈α
considered␈α
to␈α
be␈α
partial␈α
over␈α
the␈α
integers,␈α
total␈α
for␈α
the␈α
natural␈α
numbers,␈α
and␈αundefined␈α
for
␈↓ ↓H␈↓negative␈α⊂integers.␈α⊂ Thus␈α⊂the␈α⊂concept␈α⊂of␈α⊂"total"␈α⊂or␈α⊂"partial"␈α⊂is␈α⊂relative␈α⊂to␈α⊂a␈α⊂specified␈α⊃domain␈α⊂of
␈↓ ↓H␈↓discourse.␈α⊂ However,␈α⊂a␈α⊂function␈α⊂␈↓αf␈↓␈α⊂total␈α⊂over␈α⊂a␈α⊃domain␈α⊂␈↓	D␈↓β1␈↓␈α⊂can␈α⊂be␈α⊂extended␈α⊂to␈α⊂be␈α⊂total␈α⊃over␈α⊂a
␈↓ ↓H␈↓domain␈α∞␈↓	D␈↓β1␈↓∪␈↓	D␈↓β2␈↓␈α∞by␈α∞assigning␈α∞values␈α∞to␈α∞␈↓αf(d)␈↓␈α∞for␈α∞␈↓αd␈↓λε␈↓	D␈↓β2␈↓.␈α∞Thus␈α∞factorial␈α∞can␈α∞be␈α∞extended␈α∞to␈α∂be␈α∞total
␈↓ ↓H␈↓over␈α∞the␈α∞integers␈α
by␈α∞defining␈α∞␈↓αn!␈↓␈α
to␈α∞be␈α∞␈↓α0␈↓␈α
for␈α∞␈↓αn␈↓␈α∞less␈α
than␈α∞␈↓α0␈↓.␈α∞ We␈α
may␈α∞also␈α∞extend␈α
the␈α∞range␈α∞of␈α
a
␈↓ ↓H␈↓function␈α∞when␈α
we␈α∞extend␈α
the␈α∞domain;␈α
thus␈α∞␈↓αf(d)␈↓␈α∞need␈α
not␈α∞be␈α
in␈α∞the␈α
range␈α∞of␈α
the␈α∞original␈α∞␈↓αf␈↓.␈α
 For
␈↓ ↓H␈↓example,␈α
we␈αadded␈α
␈↓α0␈↓␈αto␈α
the␈αrange␈α
when␈αwe␈α
extended␈αthe␈α
factorial␈αfunction.␈α
When␈αwe␈α
extend␈αthe
␈↓ ↓H␈↓range we must specify what additions are made.

␈↓ ↓H␈↓A␈αsubstantive␈αdecision␈αneeds␈αto␈αbe␈αmade␈αon␈αhow␈αwe␈αare␈αto␈αhandle␈αpartial␈αfunctions.␈α Since␈αwe␈αare
␈↓ ↓H␈↓attempting␈α
to␈α
be␈α
reasonably␈αrealistic␈α
about␈α
our␈α
modelling␈α
of␈αcomputation␈α
we␈α
should␈α
be␈α
as␈αprecise␈α
as
␈↓ ↓H␈↓possible␈α∞in␈α∂our␈α∞formalism.␈α∂ We␈α∞could␈α∂introduce␈α∞a␈α∞class␈α∂of␈α∞error␈α∂values␈α∞and␈α∂include␈α∞them␈α∂in␈α∞the
␈↓ ↓H␈↓range␈α⊂of␈α∂␈↓αf␈↓;␈α⊂these␈α∂values␈α⊂would␈α⊂be␈α∂given␈α⊂as␈α∂the␈α⊂result␈α∂of␈α⊂applying␈α⊂␈↓αf␈↓␈α∂to␈α⊂an␈α∂argument␈α⊂not␈α⊂in␈α∂its
␈↓ ↓H␈↓␈↓↓16  Symbolic expressions␈↓ 22.4␈↓


␈↓ ↓H␈↓domain;␈α
or␈α∞we␈α
could␈α
simply␈α∞say␈α
that␈α∞the␈α
result␈α
is␈α∞"unspecified"␈α
␈↓π 7␈↓.␈α
We␈α∞shall␈α
pick␈α∞an␈α
intermediate
␈↓ ↓H␈↓position;␈αwe␈αshall␈αintroduce␈α␈↓↓one␈↓␈αnew␈αelement,␈α␈↓λB␈↓,␈αcalled␈α"unspecified"␈αor␈α"undefined",␈αor␈α"bottom"␈↓π 8␈↓.
␈↓ ↓H␈↓We␈α∞will␈α∞define␈α∞all␈α∞our␈α∞functions␈α∞over␈α∞domains␈α∞augmented␈α∞with␈α∞this␈α∞element;␈α∞thus␈α∞constructs␈α
like
␈↓ ↓H␈↓␈↓αf(␈↓λB␈↓α) = a␈↓␈α
are␈α
allowed.␈α
 Think␈α
of␈α
␈↓λB␈↓␈α∞as␈α
covering␈α
all␈α
anomalous␈α
conditions␈α
which␈α
could␈α∞be␈α
detected
␈↓ ↓H␈↓and printed as error messages.

␈↓ ↓H␈↓As␈α∩we␈α∩define␈α∩new␈α∪data␈α∩structures␈α∩we␈α∩will␈α∩frequently␈α∪want␈α∩to␈α∩extend␈α∩our␈α∩functions␈α∪to␈α∩larger
␈↓ ↓H␈↓domains.␈α∂For␈α∂most␈α∂of␈α∂our␈α∂purposes,␈α∂a␈α∂function␈α∂␈↓αf␈↓␈α∂defined␈α∂on␈α∂(an␈α∂augmented␈α∂domain)␈α∂␈↓	D␈↓␈α∂will␈α∂be
␈↓ ↓H␈↓extended␈αto␈αa␈αlarger␈αdomain,␈α␈↓	D␈↓∪␈↓	D␈↓β1␈↓,␈αby␈α
defining␈α␈↓αf(d␈↓β1␈↓α) = f(␈↓λB␈↓α)␈↓␈αfor␈α␈↓αd␈↓β1␈↓λε␈↓	D␈↓β1␈↓␈↓π 9␈↓.␈α Note␈αthat␈α␈↓αf(␈↓λB␈↓α)␈↓␈αneed␈α
not
␈↓ ↓H␈↓be␈α␈↓λB␈↓;␈αhowever␈αmany␈αof␈αthe␈αfunctions␈αwhich␈αwe␈αwill␈αexamine␈αare␈αdefined␈αsuch␈αthat␈α␈↓αf(..., ␈↓λB␈↓α, ...) = ␈↓λB␈↓;
␈↓ ↓H␈↓that␈α∂is␈α∂␈↓αf␈↓␈α∂returns␈α∂the␈α∂undefined␈α∂value␈α∂whenever␈α∂any␈α∂of␈α∂its␈α∂arguments␈α∂are␈α⊂undefined.␈α∂ Functions
␈↓ ↓H␈↓which possess this property are called ␈↓↓strict functions␈↓.

␈↓ ↓H␈↓To apply this discussion of ␈↓λB␈↓ to S-exprs we will define an extended domain ␈↓	S␈↓ to be:
␈↓ ↓H␈↓␈↓ ¬E␈↓	S␈↓ =  ␈↓<sexpr>␈↓ ∪ {␈↓λB␈↓}.

␈↓ ↓H␈↓Then␈αwe␈αcan␈αtalk␈αabout␈αfunctions␈αwhich␈αare␈αtotal␈αover␈α␈↓	S␈↓␈αor␈αtotal␈αover␈α␈↓<sexpr>␈↓,␈αand␈αwe␈αwill␈αtalk
␈↓ ↓H␈↓about␈αfunctions␈αwhich␈αare␈αpartial␈αover␈α␈↓<sexpr>␈↓.␈α
 Typically␈αwhen␈αwe␈αask␈αif␈αa␈αfunction␈αof␈α
symbolic
␈↓ ↓H␈↓expressions␈α∞is␈α∂partial␈α∞or␈α∂total␈α∞without␈α∞specifying␈α∂a␈α∞domain,␈α∂we␈α∞are␈α∞asking␈α∂the␈α∞question␈α∂over␈α∞the
␈↓ ↓H␈↓natural, unextended domain, ␈↓<sexpr>␈↓.

␈↓ ↓H␈↓The␈α
first␈α
LISP␈α
function␈α
we␈α∞consider␈α
is␈α
the␈α
␈↓αcons␈↓␈α
function␈α∞which␈α
is␈α
used␈α
to␈α
generate␈α∞S-exprs␈α
from
␈↓ ↓H␈↓less␈α
complicated␈α
S-exprs.␈α
 ␈↓αcons␈↓␈α
is␈α
called␈α
a␈α
␈↓↓constructor␈↓-function␈α
and␈α
is␈α
a␈α
strict,␈α
binary␈α
function;␈α
it␈αis␈α
a
␈↓ ↓H␈↓total␈α
function␈α
over␈α
the␈α
domain␈α
␈↓<sexpr>␈↓␈↓π 10␈↓.␈α
Thus␈α
␈↓αcons␈↓␈α
expects␈α
two␈α
arguments␈α
and␈α
gives␈α
␈↓λB␈↓␈αas␈α
value
␈↓ ↓H␈↓whenever␈αeither␈αof␈αits␈αarguments␈αis␈α␈↓λB␈↓.␈α Whenever␈α␈↓αcons␈↓␈αis␈αpresented␈αwith␈αtwo␈αelements␈α␈↓λα␈↓␈αand␈α␈↓λβ␈↓␈αof
␈↓ ↓H␈↓␈↓<sexpr>␈↓,␈α␈↓αcons[␈↓λα␈↓α;␈↓λβ␈↓α]␈↓␈αreturns␈αa␈α
new␈αS-expr␈α␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓.␈αThat␈αis,␈α
interpreted␈αas␈αa␈αLISP-tree,␈α␈↓αcons[␈↓λα␈↓α;␈↓λβ␈↓α]␈↓␈α
has
␈↓ ↓H␈↓a left branch ␈↓λα␈↓ and has a right branch ␈↓λβ␈↓.

␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓α␈↓ ¬Ocons[A; B] = (A . B)
␈↓ ↓H␈↓α␈↓ ¬⊃cons[(A . B); C] = ((A . B) .C)


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 7␈↓␈α⊂Indeed,␈α⊂how␈α⊂"unspecified"␈α⊂manifests␈α⊂itself␈α∂on␈α⊂a␈α⊂machine␈α⊂will␈α⊂depend␈α⊂on␈α⊂the␈α∂implementation.
␈↓ ↓H␈↓Sometimes error messages are given; sometimes it results in an excursion into the subconscious.

␈↓ ↓H␈↓␈↓π 8␈↓ "bottom" is sometimes written ␈↓λW␈↓.

␈↓ ↓H␈↓␈↓π 9␈↓␈α
The␈α
exception␈α
to␈αthis␈α
extension␈α
convention␈α
involves␈α
the␈αdefinition␈α
of␈α
predicates␈α
which␈α
can␈αtell
␈↓ ↓H␈↓whether␈αor␈αnot␈αan␈αarbitrary␈αelement␈αis␈αin␈αa␈αspecified␈αdomain.␈αThese␈αpredicates␈αalways␈αgive␈αtrue␈αor
␈↓ ↓H␈↓false when applied to any element other than ␈↓λB␈↓.

␈↓ ↓H␈↓␈↓π 10␈↓ What we really mean is that ␈↓↓either␈↓ argument to ␈↓αcons␈↓ can be an arbitrary S-expr.
␈↓ ↓H␈↓␈↓↓2.4␈↓ λyPrimitive Functions     17␈↓


␈↓ ↓H␈↓Expressions␈αlike␈α
the␈αabove,␈α
which␈αcan␈α
be␈αevaluated,␈α
are␈αcalled␈α
␈↓↓forms␈↓.␈α S-exprs␈α
are␈αforms␈αsince␈α
they
␈↓ ↓H␈↓are␈α
the␈α
constants␈α
of␈α
our␈α
language:␈α
the␈αvalue␈α
of␈α
a␈α
constant␈α
is␈α
that␈α
constant.␈α
 Function␈αapplications
␈↓ ↓H␈↓are␈α∀forms:␈α∪the␈α∀value␈α∀is␈α∪the␈α∀result␈α∀of␈α∪performing␈α∀the␈α∀designated␈α∪function␈α∀on␈α∀the␈α∪designated
␈↓ ↓H␈↓arguments.

␈↓ ↓H␈↓Notice␈αthat␈αwe␈αare␈α
designating␈αfunction␈αapplication␈αin␈αLISP␈α
by␈α"function␈αname,␈αfollowed␈αby␈α
a␈αlist
␈↓ ↓H␈↓of␈αarguments␈αdelimited␈αby␈α
`['␈αand␈α`]'␈↓π 11␈↓."␈αThe␈α
`[...]'-notation␈αis␈αpart␈αof␈α
the␈αLISP␈αsyntax␈αand␈α
we␈αwill
␈↓ ↓H␈↓reserve␈α`(...)'-notation␈αfor␈α
the␈αfunction␈αapplication␈αof␈α
mathematics.␈αIn␈αa␈α
few␈αplaces␈αin␈αour␈α
discussions
␈↓ ↓H␈↓the␈αdistinction␈αwill␈αbe␈αimportant.␈α Typically␈αthe␈αdistinctions␈αwill␈αoccur␈αwhen␈αwe␈αwish␈αto␈αdistinguish
␈↓ ↓H␈↓between the LISP algorithm and the mathematical function computed by that algorithm.

␈↓ ↓H␈↓We␈αhave␈αtwo␈αstrict,␈αunary␈α␈↓↓selector␈↓␈αfunctions,␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓␈↓π 12␈↓,␈αfor␈αtraversing␈αLISP-trees.␈α We␈αalready
␈↓ ↓H␈↓know␈αthe␈α
meaning␈αof␈α
"strict";␈αa␈α
unary␈αfunction␈αexpects␈α
␈↓↓one␈↓␈αargument;␈α
and␈αa␈α
selector␈αfunction␈α
is␈αa
␈↓ ↓H␈↓data␈α
structure␈α
manipulating␈αfunction␈α
which␈α
will␈αselect␈α
a␈α
component␈αof␈α
a␈α
composite␈α
data␈αstructure.
␈↓ ↓H␈↓Both␈α
␈↓αcar␈↓␈α
and␈α
␈↓αcdr␈↓␈αare␈α
selectors␈α
since␈α
they␈α
will␈αselect␈α
components␈α
of␈α
non-atomic␈α
elements␈αof␈α
␈↓<sexpr>␈↓.
␈↓ ↓H␈↓Thus␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓␈αare␈αboth␈αpartial␈αfunctions␈αover␈α␈↓<sexpr>␈↓;␈αthey␈αgive␈αvalues␈αin␈α␈↓<sexpr>␈↓␈αonly␈αfor
␈↓ ↓H␈↓non-atomic arguments; they give ␈↓λB␈↓ whenever they are presented with an atomic argument.

␈↓ ↓H␈↓When␈α∞given␈α∞a␈α∞non-atomic␈α∞argument,␈α∞␈↓α(␈↓λα␈↓α␈α∞.␈α∞␈↓λβ␈↓α)␈↓,␈α∞␈↓αcar␈↓␈α∞returns␈α∞as␈α∞value␈α∞the␈α∞first␈α∞subexpression,␈α∞␈↓λα␈↓;␈α
␈↓αcdr␈↓
␈↓ ↓H␈↓(pronounced could-er) returns as value the second sub-expression ␈↓λβ␈↓.

␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓␈↓ ∧|␈↓αcar[(A . B)] = A        car[A] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ∧"cdr[(A . B)] = B       cdr[(A .(B . C))] = (B . C)
␈↓ ↓H␈↓α␈↓ ¬&car[((A . B) . C)] = (A . B)

␈↓ ↓H␈↓As␈αwith␈αmost␈αmathematical␈αtheories,␈αwe␈αwill␈αallow␈αfunctional␈αcomposition.␈α The␈αcomposition␈αof␈αtwo
␈↓ ↓H␈↓unary␈α
functions␈α
␈↓αf␈↓␈α
and␈α
␈↓αg␈↓␈αis␈α
another␈α
function,␈α
sometimes␈α
denoted␈αby␈α
␈↓αf␈↓∞o␈↓αg␈↓.␈α
The␈α
value␈α
of␈αan␈α
expression,
␈↓ ↓H␈↓␈↓αf␈↓∞o␈↓αg[x]␈↓,␈α
is␈α
the␈α
value␈α
of␈α
␈↓αf[g[x]]␈↓.␈α
 That␈α
is,␈α
the␈α
value␈α
of␈α
␈↓αf␈↓∞o␈↓αg[x]␈↓␈α
is␈α
a␈α
␈↓αz␈↓␈α
such␈α
that␈α
␈↓αy␈↓␈α
is␈α
the␈α
value␈α
of␈α
␈↓αg[x]␈↓␈α
and
␈↓ ↓H␈↓␈↓αz␈↓␈α
is␈αthe␈α
value␈αof␈α
␈↓αf[y]␈↓.␈α
␈↓αf␈↓∞o␈↓αg␈↓␈αmay␈α
be␈αundefined␈α
for␈αseveral␈α
reasons:␈α
␈↓αg[x]␈↓␈αmay␈α
be␈αundefined,␈α
or␈α␈↓αf[y]␈↓␈α
may
␈↓ ↓H␈↓be undefined.







␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 11␈↓ The syntax equations for forms are given on page 20.

␈↓ ↓H␈↓␈↓π 12␈↓␈α
These␈α
names␈α
are␈α
hold-overs␈αfrom␈α
the␈α
original␈α
implementation␈α
of␈αLISP␈α
on␈α
an␈α
IBM␈α
704.␈αThat
␈↓ ↓H␈↓machine␈αhad␈αpartial-word␈α
instructions␈αto␈αreference␈α
the␈α␈↓a␈↓ddress␈αand␈α
␈↓d␈↓ecrement␈αparts␈αof␈α
a␈αmachine
␈↓ ↓H␈↓location.␈α
The␈α
␈↓αa␈↓␈α
of␈α
␈↓αcar␈↓␈α
comes␈α
from␈α
"address",␈α
the␈α
␈↓αd␈↓␈α
of␈α
␈↓αcdr␈↓␈α
comes␈α
from␈α
"decrement".␈α
The␈α
␈↓αc␈↓␈α
and␈α
␈↓αr␈↓␈α
come
␈↓ ↓H␈↓from "contents of" and "register". Thus ␈↓αcar␈↓ could be read "contents of address part of register".
␈↓ ↓H␈↓␈↓↓18  Symbolic expressions␈↓ 22.4␈↓



␈↓ ↓H␈↓Here are some examples of composition:

␈↓ ↓H␈↓α␈↓ β=car␈↓∞o␈↓αcdr[(A .(B . C))] = car[cdr[(A .(B . C))]] = car[(B . C)] =  B
␈↓ ↓H␈↓α␈↓ β;cdr␈↓∞o␈↓αcdr[(A .(C . B))] = cdr[cdr[(A .(C . B))]] = cdr[(C . B)] =  B
␈↓ ↓H␈↓α␈↓ ¬lcdr[cdr[A]] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ¬Jcar[cdr[(A . B)]] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ∧<car[cons[X;A]] = X     cdr[cons[Y;X]] = X .

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αcompositions␈αwhich␈αgive␈αresult␈α
␈↓λB␈↓␈αdo␈αso␈αby␈αresorting␈αto␈αour␈αcharacterization␈α
of␈α␈↓αcar␈↓
␈↓ ↓H␈↓and ␈↓αcdr␈↓ as strict functions which have been extended to ␈↓	S␈↓.

␈↓ ↓H␈↓The␈αcomposition␈αof␈α
many␈α␈↓αcar␈↓␈αand␈α␈↓αcdr␈↓␈α
functions␈αoccurs␈αso␈αfrequently␈α
that␈αan␈αabbreviation␈αhas␈α
been
␈↓ ↓H␈↓developed.␈α
 Given␈αsuch␈α
a␈αcomposition,␈α
we␈αselect␈α
in␈α
left-to-right␈αorder,␈α
the␈αrelevant␈α
␈↓αa␈↓'s␈αand␈α
␈↓αd␈↓'s␈αin␈α
the
␈↓ ↓H␈↓␈↓αcar␈↓'s␈α
and␈α
␈↓αcdr␈↓'s.␈αWe␈α
sandwich␈α
this␈αstring␈α
of␈α
␈↓αa␈↓'s␈αand␈α
␈↓αd␈↓'s␈α
between␈αa␈α
left-hand␈α
␈↓αc␈↓␈αand␈α
a␈α
right-hand␈α␈↓αr␈↓␈α
and
␈↓ ↓H␈↓give the composition this name.
␈↓ ↓H␈↓For example:
␈↓ ↓H␈↓α␈↓ ¬Gcadr[x] <= car[cdr[x]]
␈↓ ↓H␈↓α␈↓ ¬!caddr[x] <= car[cdr[cdr[x]]]
␈↓ ↓H␈↓α␈↓ ¬Gcdar[x] <= cdr[car[x]]

␈↓ ↓H␈↓These␈α⊂compositions␈α⊂are␈α∂also␈α⊂called␈α⊂␈↓↓␈↓αcar-cdr-␈↓↓chains␈↓,␈α∂and␈α⊂are␈α⊂useful␈α∂in␈α⊂traversing␈α⊂LISP-trees.␈α∂The
␈↓ ↓H␈↓"<="-␈α
notation␈α
is␈α
to␈α
be␈α
read␈α
"is␈α
defined␈α
to␈α
be␈α
the␈α
function␈α
...".␈α
 This␈α
notation␈α
is␈α
only␈α∞a␈α
temporary
␈↓ ↓H␈↓convenience␈αand␈αnot␈αpart␈αof␈αLISP.␈α Very␈αsoon␈αwe␈αwill␈αstudy␈αwhat␈αis␈αinvolved␈αin␈αgiving␈αand␈αusing
␈↓ ↓H␈↓definitions in LISP (Section 4.4).  For the moment intuition should suffice.

␈↓ ↓H␈↓Notice␈α⊃that␈α⊃with␈α⊃these␈α⊃definitions␈α⊃we␈α⊂have␈α⊃introduced␈α⊃variables␈α⊃(over␈α⊃S-exprs).␈α⊃ In␈α⊃the␈α⊂sequel
␈↓ ↓H␈↓lower-case␈αidentifiers␈↓π 13␈↓␈α
will␈αbe␈α
used␈αfreely␈α
as␈αvariables.␈α So␈α
for␈αexample␈α
␈↓αY␈↓␈αand␈α
␈↓αCAR␈↓␈αare␈α
atoms;␈α␈↓αy␈↓
␈↓ ↓H␈↓and␈α␈↓αcar␈↓␈αcould␈αbe␈αused␈αas␈αvariables.␈α Be␈αclear␈αon␈αthe␈αdistinction␈αbetween␈αLISP␈αvariables␈αlike␈α␈↓αx,␈αy␈↓␈α
or
␈↓ ↓H␈↓␈↓αfoo␈↓,␈αand␈α
the␈αmatch␈α
variables␈↓π 14␈↓␈αlike␈α
␈↓λα␈↓␈αor␈α
␈↓λβ␈↓.␈α For␈α␈↓λα␈↓␈α
and␈α␈↓λβ␈↓␈α
ranging␈αover␈α
S-expressions,␈α␈↓α(␈↓λα␈↓α . ␈↓λβ␈↓α)␈↓␈α
is␈αa
␈↓ ↓H␈↓well formed S-expr. The construction, ␈↓α(x . y)␈↓, is ␈↓↓not␈↓ well-formed, but ␈↓αcons[x;y]␈↓ is correct.

␈↓ ↓H␈↓It␈α∞␈↓↓is␈↓␈α∞useful␈α∞now,␈α∂however,␈α∞to␈α∞introduce␈α∞some␈α∞terminology␈α∂for␈α∞talking␈α∞about␈α∞the␈α∞components␈α∂of␈α∞a
␈↓ ↓H␈↓function definition. Let
␈↓ ↓H␈↓α␈↓ ¬gf[x␈↓β1␈↓α; ...; x␈↓βn␈↓α] <= ␈↓λx␈↓α

␈↓ ↓H␈↓represent␈α⊂a␈α⊂typical␈α⊂definition.␈α⊂The␈α⊂␈↓↓name␈↓␈α⊂of␈α⊂the␈α⊂function␈α⊂is␈α⊂␈↓αf␈↓;␈α⊂the␈α⊂␈↓↓body␈↓␈α⊂of␈α⊂the␈α⊂function␈α⊂is␈α⊂the
␈↓ ↓H␈↓expression␈α∩␈↓λx␈↓.␈α⊃ The␈α∩list␈α⊃of␈α∩variables␈α∩appearing␈α⊃after␈α∩the␈α⊃function␈α∩name␈α⊃are␈α∩called␈α∩the␈α⊃␈↓↓formal
␈↓ ↓H␈↓↓parameters␈↓.  A function is ␈↓↓applied␈↓ using the common notation of ␈↓↓function application␈↓:
␈↓ ↓H␈↓α␈↓ ε	f[a␈↓β1␈↓α; ...; a␈↓βn␈↓α]


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 13␈↓ See page 20 for the BNF equations for <identifier>.

␈↓ ↓H␈↓␈↓π 14␈↓ also called meta-variables
␈↓ ↓H␈↓␈↓↓2.4␈↓ λyPrimitive Functions     19␈↓


␈↓ ↓H␈↓The␈α␈↓αa␈↓βi␈↓'s␈α
are␈αcalled␈α␈↓↓actual␈α
parameters␈↓;␈αfor␈αan␈α
application␈αto␈αbe␈α
well␈αformed,␈αthe␈α
actual␈αparameters
␈↓ ↓H␈↓must␈α⊃agree␈α⊃in␈α⊃number␈α⊃with␈α⊃the␈α⊃formal␈α⊃parameters␈α∩of␈α⊃the␈α⊃definition␈α⊃of␈α⊃␈↓αf␈↓␈α⊃and␈α⊃they␈α⊃are␈α∩to␈α⊃be
␈↓ ↓H␈↓associated␈α∂in␈α∞a␈α∂one-for-one␈α∞order,␈α∂␈↓αa␈↓βi␈↓␈α∞with␈α∂␈↓αx␈↓βi␈↓.␈α∞ Thus␈α∂in␈α∞the␈α∂expression␈α∞␈↓αcar[cdr[(A . B)]]␈↓␈α∂the␈α∞actual
␈↓ ↓H␈↓parameter␈α
to␈αthe␈α
␈↓αcar␈↓␈αfunction␈α
is␈α␈↓αcdr[(A . B)]␈↓,␈α
and␈α
the␈αactual␈α
parameter␈αto␈α
␈↓αcdr␈↓␈αis␈α
␈↓α(A . B)␈↓.␈α
 How␈αthe
␈↓ ↓H␈↓actual parameters are to be treated will be a large part of our study.

␈↓ ↓H␈↓Once␈α∩again␈α∪we␈α∩are␈α∪getting␈α∩a␈α∪hint␈α∩of␈α∪differences␈α∩between␈α∪mathematics␈α∩and␈α∪computer␈α∩science.
␈↓ ↓H␈↓Mathematically␈αspeaking,␈αa␈αcomposition␈αof␈αfunctions␈αis␈αsimply␈αanother␈αfunction␈α-- i.e., a␈αmapping --
␈↓ ↓H␈↓and␈α↔therefore␈α↔nothing␈α↔need␈α↔be␈α↔said␈α↔about␈α↔how␈α↔to␈α↔compute␈α↔composed␈α↔functions.␈α↔From␈α↔a
␈↓ ↓H␈↓computational␈α⊂point␈α∂of␈α⊂view,␈α∂we␈α⊂want␈α∂to␈α⊂express␈α∂evaluation␈α⊂of␈α∂expressions␈α⊂involving␈α∂composed
␈↓ ↓H␈↓functions␈αin␈αterms␈αof␈αthe␈αevaluation␈αof␈αsubexpressions.␈αThis␈αwould␈αallow␈αus␈αto␈αdescribe␈αa␈αcomplex
␈↓ ↓H␈↓computation␈α∂in␈α∞terms␈α∂of␈α∂an␈α∞appropriate␈α∂sequence␈α∞of␈α∂subsidiary␈α∂computations.␈α∞ One␈α∂of␈α∂the␈α∞more
␈↓ ↓H␈↓natural␈α↔ways␈α↔to␈α⊗evaluate␈α↔expressions␈α↔involving␈α↔compositions␈α⊗is␈α↔to␈α↔evaluate␈α↔the␈α⊗inner-most
␈↓ ↓H␈↓expressions␈α
first,␈α
then␈α
work␈α
outwards.␈α
Assume␈α
arguments␈α
to␈α
multi-argument␈α
functions␈αare␈α
evaluated
␈↓ ↓H␈↓in left-to-right order. Thus:

␈↓ ↓H␈↓α␈↓ αXcons[car[(A . B)];cdr[(A . (1 . 2))]]␈↓ πλ=  cons[A;cdr[(A . (1 . 2))]]
␈↓ ↓H␈↓α␈↓ αX␈↓ πλ=  cons[A;(1 . 2)]
␈↓ ↓H␈↓α␈↓ αX␈↓ πλ=  (A .(1 . 2))

␈↓ ↓H␈↓Evaluation␈α∞may␈α∂seem␈α∞to␈α∂be␈α∞a␈α∂simple␈α∞operation␈α∞but␈α∂looks␈α∞can␈α∂be␈α∞deceiving;␈α∂evaluation␈α∞is␈α∂a␈α∞very
␈↓ ↓H␈↓complex process.  The value of an expression may depend on the ␈↓↓order␈↓ in which we do things.

␈↓ ↓H␈↓For␈α⊃example␈α⊃consider␈α⊃the␈α⊃evaluation␈α⊃of␈α⊃␈↓αsecond[car[A]; B]␈↓␈α⊃where␈α⊃␈↓αsecond[x;y] <= y␈↓.␈α⊃ If␈α⊃we␈α⊃expect
␈↓ ↓H␈↓␈↓αsecond␈↓␈αto␈αbe␈αa␈αstrict␈αfunction,␈αthen␈α␈↓αsecond[car[A]; B]␈↓␈αmust␈αreturn␈α␈↓λB␈↓␈αeven␈αthough␈αit␈αis␈αreasonable␈αto
␈↓ ↓H␈↓believe␈α
that␈α
the␈α
value␈α
of␈αthe␈α
computation␈α
should␈α
be␈α
␈↓αB␈↓␈α
since␈α␈↓αsecond␈↓␈α
does␈α
not␈α
depend␈α
on␈α
the␈αvalue␈α
of
␈↓ ↓H␈↓its␈α∞first␈α∞parameter.␈α∞ It␈α∂appears␈α∞that␈α∞if␈α∞we␈α∞postponed␈α∂the␈α∞evaluation␈α∞of␈α∞the␈α∞arguments␈α∂until␈α∞those
␈↓ ↓H␈↓values␈α↔were␈α_actually␈α↔needed,␈α_then␈α↔at␈α_least␈α↔this␈α_problem␈α↔would␈α_be␈α↔solved.␈α_ However,␈α↔the
␈↓ ↓H␈↓consequences␈αof␈αdefining␈αa␈αfunction␈αto␈αbe␈αstrict␈αare␈αsevere;␈αthey␈αcannot␈αbe␈αsidestepped␈αby␈αresorting
␈↓ ↓H␈↓to␈α∀different␈α∪schemes␈α∀for␈α∪evaluating␈α∀arguments.␈α∀There␈α∪is␈α∀an␈α∪alternative␈α∀strategy␈α∀in␈α∪assigning
␈↓ ↓H␈↓strictness:␈α
we␈αcould␈α
examine␈αthe␈α
body␈αof␈α
the␈αfunction;␈α
if␈αthe␈α
function␈αuses␈α
all␈αits␈α
parameters,␈αthen
␈↓ ↓H␈↓it's␈αstrict.␈αIf␈αthe␈αfunction␈αdoesn't␈αdepend␈αon␈αone␈αor␈αmore␈αparameters,␈αthen␈αit's␈αnon-strict.␈αThus␈αwith
␈↓ ↓H␈↓this␈α⊂interpretation,␈α∂␈↓αsecond␈↓␈α⊂␈↓↓is␈↓␈α⊂non-strict.␈α∂We␈α⊂prefer␈α∂the␈α⊂initial␈α⊂interpretation,␈α∂reasoning␈α⊂that,␈α⊂if␈α∂a
␈↓ ↓H␈↓function␈α
is␈αpassed␈α
bad␈αinformation,␈α
then␈αwe␈α
wish␈αto␈α
know␈αabout␈α
it,␈αeven␈α
if␈αthe␈α
function␈α
does␈αnot
␈↓ ↓H␈↓use that specious result.

␈↓ ↓H␈↓Strictness␈α∂is␈α∂closely␈α∂related␈α∂to␈α∂evaluation␈α∞schemes␈α∂for␈α∂parameter␈α∂passing.␈α∂ Here␈α∂are␈α∂two␈α∞common
␈↓ ↓H␈↓techniques:

␈↓ ↓H␈↓␈↓	CBV␈↓␈↓ α(Evaluate the arguments to a function; pass those evaluated arguments to the function.

␈↓ ↓H␈↓This␈α∂scheme,␈α⊂called␈α∂␈↓	C␈↓all␈α⊂␈↓	B␈↓y␈α∂␈↓	V␈↓alue,␈α⊂is␈α∂what␈α⊂we␈α∂were␈α⊂informally␈α∂using␈α⊂to␈α∂evaluate␈α⊂the␈α∂previous
␈↓ ↓H␈↓examples.
␈↓ ↓H␈↓␈↓↓20  Symbolic expressions␈↓ 22.4␈↓



␈↓ ↓H␈↓An alternative evaluation process is ␈↓	C␈↓all ␈↓	B␈↓y ␈↓	N␈↓ame:

␈↓ ↓H␈↓␈↓	CBN␈↓␈↓ α(Pass the unevaluated arguments into the body of the function.

␈↓ ↓H␈↓Assuming␈α␈↓αsecond␈↓␈α
is␈αdefined␈α
to␈αbe␈α
strict,␈αthen␈α
␈↓αsecond[car[A]; B]␈↓␈αyields␈α
␈↓λB␈↓␈αunder␈α
either␈α␈↓	CBV␈↓␈αor␈α
␈↓	CBN␈↓.
␈↓ ↓H␈↓However␈α
if␈α
we␈αdefine␈α
␈↓αsecond␈↓␈α
to␈αbe␈α
non-strict␈α
then␈α
␈↓	CBV␈↓␈αand␈α
␈↓	CBN␈↓␈α
will␈αboth␈α
give␈α
value␈α
␈↓αB␈↓.␈α With
␈↓ ↓H␈↓␈↓	CBV␈↓, ␈↓αx␈↓ is bound to ␈↓λB␈↓; while with ␈↓	CBN␈↓ ␈↓αx␈↓ is bound to ␈↓αcar[A]␈↓.

␈↓ ↓H␈↓Further␈α
relationships␈α
between␈α
evaluation␈α
schemes␈αand␈α
strictness␈α
will␈α
be␈α
investigated.␈α
 On␈αpage 24
␈↓ ↓H␈↓we␈αdiscuss␈αnon-terminating␈αcomputations.␈α In␈α
Chapter 4␈αwe␈αwill␈αdiscuss␈αevaluation␈α
techniques␈αand
␈↓ ↓H␈↓will␈α∂give␈α∞a␈α∂precise␈α∞characterization␈α∂of␈α∂the␈α∞evaluation␈α∂of␈α∞LISP␈α∂expressions.␈α∞ On␈α∂page 23␈α∂we␈α∞will
␈↓ ↓H␈↓introduce␈α∂a␈α∂non-strict␈α∂language␈α∂construct␈α∂but,␈α⊂until␈α∂that␈α∂time,␈α∂intuitive␈α∂application␈α∂of␈α⊂␈↓	CBV␈↓␈α∂will
␈↓ ↓H␈↓suffice.

␈↓ ↓H␈↓What␈αshould␈αbe␈αkept␈αin␈αmind␈αfrom␈αthis␈αdiscussion␈αis␈αthat␈αwe␈αmust␈αbe␈αcareful␈αwhen␈αdiscussing␈αthe
␈↓ ↓H␈↓process␈αof␈αevaluation;␈αthe␈αfunction␈αwe␈αare␈αcharacterizing␈αby␈αcomputing␈αits␈αvalues␈αwill␈αoften␈αdepend
␈↓ ↓H␈↓on our choice of evaluation scheme.

␈↓ ↓H␈↓Before␈α⊂introducing␈α⊂a␈α⊃further␈α⊂class␈α⊂of␈α⊃LISP␈α⊂expressions␈α⊂we␈α⊂summarize␈α⊃the␈α⊂syntax␈α⊂of␈α⊃the␈α⊂LISP
␈↓ ↓H␈↓expressions (or forms) allowed so far:

␈↓ ↓H␈↓<form>␈↓ βH::= <constant> | <application> |  <variable>

␈↓ ↓H␈↓<constant>␈↓ βH::= <sexpr>

␈↓ ↓H␈↓<application>␈↓ βH::= <function-part>[<arg>; ...;<arg>]

␈↓ ↓H␈↓<function-part>␈↓ βH::= <identifier>

␈↓ ↓H␈↓<arg>␈↓ βH::= <form>

␈↓ ↓H␈↓<variable>␈↓ βH::= <identifier>

␈↓ ↓H␈↓<identifier>␈↓ βH::= <letter> | <identifier><letter> | <identifier><digit>

␈↓ ↓H␈↓<letter>␈↓ βH::= ␈↓αa | b | c  ... | z


␈↓ ↓H␈↓The␈α
use␈αof␈α
ellipses␈αin␈α
the␈αlast␈α
equation␈αis␈α
an␈α
abbreviation␈αwe␈α
have␈αseen␈α
before.␈α The␈α
use␈αof␈α
ellipses
␈↓ ↓H␈↓in␈α
the␈α
<application>␈α
equation␈α
is␈α
different.␈α
It␈α
is␈α
an␈α
abbreviation␈α
meaning␈α
"zero␈α
or␈α
more␈α
occurrences".
␈↓ ↓H␈↓Thus␈α
the␈α
equation␈αmeans␈α
an␈α
<application>␈αis␈α
a␈α
<function-part>␈α
followed␈αby␈α
the␈α
symbol␈α"["␈α
followed
␈↓ ↓H␈↓by␈αzero␈αor␈αmore␈α<arg>'s␈αfollowed␈αby␈αthe␈αsymbol␈α"]".␈α This␈αuse␈αof␈αellipses␈αcan␈αalways␈αbe␈αreplaced␈αby
␈↓ ↓H␈↓a sequence of BNF equations. for example, this instance can be replaced by:
␈↓ ↓H␈↓␈↓↓2.4␈↓ λyPrimitive Functions     21␈↓


␈↓ ↓H␈↓<application>␈↓ βH::= <function-part>[<arg-list>] |  <function-part>[ ]
␈↓ ↓H␈↓<arg-list>␈↓ βH::= <arg> | <arglist>;<arg>

␈↓ ↓H␈↓To␈α⊂increase␈α⊂lucidity␈α⊂we␈α⊂will␈α⊂frequently␈α⊂violate␈α⊂these␈α⊂syntax␈α⊂equations,␈α⊂allowing␈α⊃function␈α⊂names
␈↓ ↓H␈↓containing␈αspecial␈αcharacters,␈αe.g.␈α␈↓αfact*␈↓,␈α␈↓αfib␈↓λ'␈↓␈αor␈α+␈α;␈αor␈αwriting␈α␈↓αx+y␈↓␈αinstead␈αof␈α␈↓α+[x;y]␈↓.␈α No␈αattempt␈αwill
␈↓ ↓H␈↓be made to characterize these violations; occurrences of them should be clear from context.

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αclass␈α<form>␈αis␈αa␈αcollection␈αof␈αLISP␈αexpressions␈αwhich␈αcan␈αbe␈αevaluated.␈αA␈α<form>
␈↓ ↓H␈↓is either:

␈↓ ↓H␈↓␈↓ α_␈↓↓1.␈↓ a constant: the value is that constant.

␈↓ ↓H␈↓␈↓ α_␈↓↓2.␈↓ an application: we've said a bit about evaluation schemes for these constructs.

␈↓ ↓H␈↓␈↓ α_␈↓↓3.␈↓ a variable: a variable in LISP will typically have an associated value in some environment.

␈↓ ↓H␈↓Again we will wait to Section 4.4 for a precise description.

␈↓ ↓H␈↓An␈α⊃important␈α⊃constraint␈α⊃on␈α⊃LISP␈α⊃forms␈α⊃which␈α⊃is␈α⊃not␈α⊃covered␈α⊃by␈α⊃the␈α⊃syntax␈α⊃equations␈α⊃is␈α⊂the
␈↓ ↓H␈↓requirement␈αthat␈αfunctions␈αare␈αdefined␈αas␈αbeing␈αn-ary␈αfor␈αsome␈α␈↓↓fixed␈↓␈αn.␈α Any␈αn-ary␈αfunction␈αmust
␈↓ ↓H␈↓have␈α
␈↓↓exactly␈↓␈αn␈α
arguments␈αpresented␈α
to␈α
it␈αwhenever␈α
it␈αis␈α
applied.␈α Thus␈α
␈↓αcons[A],␈α
cons[A;B;C],␈α␈↓and
␈↓ ↓H␈↓␈↓αcar[A;B]␈↓ are all ill-formed expressions and therefore denote ␈↓λB␈↓.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.  Discuss ␈↓αcons[car[x];cdr[x]] = x␈↓.

␈↓ ↓H␈↓II. Discuss ␈↓αcons[car[␈↓λα␈↓α];cdr[␈↓λα␈↓α]] = ␈↓λα␈↓.



␈↓ ↓H␈↓␈↓ ∧)␈↓↓2.5  Predicates and Conditional Expressions␈↓


␈↓ ↓H␈↓We␈α∂cannot␈α∂generate␈α∂a␈α∂very␈α∂exciting␈α∂theory␈α⊂based␈α∂simply␈α∂on␈α∂␈↓αcar,␈α∂cdr,␈↓␈α∂and␈α∂␈↓αcons␈α∂␈↓␈α⊂with␈α∂functional
␈↓ ↓H␈↓composition.␈α⊂Before␈α⊂we␈α⊂can␈α∂write␈α⊂reasonably␈α⊂interesting␈α⊂algorithms␈α∂we␈α⊂must␈α⊂have␈α⊂some␈α⊂way␈α∂of
␈↓ ↓H␈↓performing␈α
conditional␈αactions.␈α
 To␈αdo␈α
this␈α
we␈αfirst␈α
need␈αpredicates.␈α
A␈α
LISP␈αpredicate␈α
is␈αa␈α
function
␈↓ ↓H␈↓returning␈αa␈αvalue␈αrepresenting␈αtruth␈αor␈αfalsity.␈α We␈αwill␈αrepresent␈αthe␈αconcepts␈αof␈αtrue␈αand␈αfalse␈αby
␈↓ ↓H␈↓␈↓
t␈↓␈αand␈α␈↓
f␈↓␈αrespectively.␈αSince␈αthese␈αtruth␈αvalues␈αare␈αdistinct␈αfrom␈αelements␈αof␈α␈↓	S␈↓,␈αwe␈αwill␈αset␈αup␈αa␈αnew
␈↓ ↓H␈↓domain␈α␈↓	Tr␈↓␈α
which␈αwill␈αconsist␈α
of␈αthe␈αelements,␈α
␈↓
t␈↓␈αand␈α
␈↓
f␈↓.␈αAs␈αusual␈α
the␈αextra␈αelement␈α
␈↓λB␈↓␈αis␈αincluded␈α
so
␈↓ ↓H␈↓␈↓↓22  Symbolic expressions␈↓ 42.5␈↓


␈↓ ↓H␈↓that we may talk about partial predicates just as we talked about partial functions on ␈↓<sexpr>␈↓.␈↓π 15␈↓.

␈↓ ↓H␈↓LISP␈αhas␈αtwo␈αprimitive␈αpredicates.␈α
 The␈αfirst␈αis␈αa␈αstrict␈α
unary␈αpredicate␈αnamed␈α␈↓αatom␈↓;␈α␈↓αatom␈↓␈α
is␈αtotal
␈↓ ↓H␈↓over␈α∀␈↓<sexpr>␈↓,␈α∃and␈α∀is␈α∀a␈α∃special␈α∀kind␈α∀of␈α∃predicate␈α∀called␈α∀a␈α∃␈↓↓recognizer␈↓␈α∀or␈α∃a␈α∀␈↓↓discriminator␈↓.
␈↓ ↓H␈↓Recognizers␈α∂are␈α∂used␈α∂to␈α∂determine␈α∂the␈α∂type␈α⊂of␈α∂an␈α∂instance␈α∂of␈α∂a␈α∂data␈α∂structure.␈α∂ Thus␈α⊂␈↓αatom␈↓␈α∂will
␈↓ ↓H␈↓return ␈↓
t␈↓ if the argument denotes an atom, and will return ␈↓
f␈↓ if the argument is a non-atomic S-expr.

␈↓ ↓H␈↓α␈↓ ¬.atom[A] = atom[NIL] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ¬←atom[(A . B)]  = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ¬Batom[car[(A . B)]]  = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ¬⎇atom[␈↓λB␈↓α] = ␈↓λB␈↓α

␈↓ ↓H␈↓What␈α
should␈α
we␈α
do␈α
about␈α
the␈α
value␈α
of␈α
constructs␈α
like:␈α
␈↓αcons[atom[A]; A]␈↓?␈α
␈↓αatom[A]␈↓␈α
gives␈α
␈↓
t␈↓,␈α
but␈α∞␈↓
t␈↓␈α
is
␈↓ ↓H␈↓not␈α
an␈α
element␈α
of␈α
␈↓	S␈↓␈α
and␈αthus␈α
is␈α
not␈α
appropriate␈α
as␈α
an␈αargument␈α
to␈α
␈↓αcons␈↓.␈α
Using␈α
our␈α
argument␈αof
␈↓ ↓H␈↓page 16 we extend the domains of the S-expr primitives to
␈↓ ↓H␈↓␈↓ εα␈↓	S␈↓β1␈↓ = ␈↓	S␈↓∪␈↓	Tr␈↓
␈↓ ↓H␈↓α␈↓For example:␈↓α
␈↓ ↓H␈↓α␈↓ ∧car[s] = car[␈↓λB␈↓α],   cons[s; A] = cons[␈↓λB␈↓α; A]␈↓ for ␈↓αs␈↓λε␈↓	Tr

␈↓ ↓H␈↓Since all those functions were strict with respect to undefined we have:
␈↓ ↓H␈↓α␈↓ ε∧atom[␈↓
t␈↓α] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ¬mcons[␈↓λB␈↓α; A] = ␈↓λB␈↓

␈↓ ↓H␈↓Notice␈αthat␈α
we␈αnow␈αhave␈α
␈↓↓two␈↓␈αseparate␈αdomains:␈α
S-expressions␈αand␈αtruth␈α
values.␈α Since␈αwe␈α
will␈αbe
␈↓ ↓H␈↓writing␈α
functions␈αover␈α
several␈αdomains␈α
we␈αwill␈α
need␈αa␈α
general␈αrecognizer␈α
for␈αeach␈α
domain␈αto␈α
assure
␈↓ ↓H␈↓that␈α
the␈α
operations␈α
defined␈αon␈α
each␈α
abstract␈α
data␈α
structure␈αare␈α
properly␈α
applied.␈α
Thus␈αwe␈α
introduce
␈↓ ↓H␈↓the␈αrecognizer␈α␈↓αissexpr␈↓␈αwhich␈αwill␈αgive␈α␈↓
t␈↓␈αon␈αthe␈αdomain␈αof␈αS-exprs,␈αand␈αwill␈αgive␈α␈↓
f␈↓␈αfor␈αany␈αelement
␈↓ ↓H␈↓other than ␈↓λB␈↓.
␈↓ ↓H␈↓α␈↓ ¬issexpr[(A . B)] = issexpr[A] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ¬{issexpr[␈↓
t␈↓α] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ¬nissexpr[␈↓λB␈↓α] = ␈↓λB␈↓α

␈↓ ↓H␈↓Another␈α
primitive␈α
predicate␈α
we␈α
need␈α
is␈α
named␈α
␈↓αeq␈↓.␈α It␈α
is␈α
a␈α
strict␈α
binary␈α
predicate,␈α
partial␈α
over␈αthe␈α
set
␈↓ ↓H␈↓␈↓<sexpr>␈↓;␈α⊂it␈α⊂will␈α∂give␈α⊂a␈α⊂truth␈α∂value␈α⊂only␈α⊂if␈α∂its␈α⊂arguments␈α⊂are␈α∂both␈α⊂atomic.␈α⊂ It␈α∂returns␈α⊂␈↓
t␈↓␈α⊂if␈α∂the
␈↓ ↓H␈↓arguments␈α∂denote␈α⊂the␈α∂same␈α∂atom;␈α⊂it␈α∂returns␈α∂␈↓
f␈↓␈α⊂if␈α∂the␈α∂arguments␈α⊂represent␈α∂different␈α∂atoms.␈α⊂ ␈↓αeq␈↓␈α∂is
␈↓ ↓H␈↓extended to ␈↓	S␈↓β1␈↓ to yield ␈↓λB␈↓ if either argument to ␈↓αeq␈↓ denotes an element not in the set ␈↓<atom>␈↓.



␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 15␈↓␈α
A␈αword␈α
for␈α
the␈αinveterate␈α
LISP␈α
hacker:␈αour␈α
use␈αof␈α
␈↓
t␈↓␈α
and␈α␈↓
f␈↓␈α
marks␈α
our␈αfirst␈α
major␈α
break␈αfrom
␈↓ ↓H␈↓current␈αLISP␈αfolklore.␈α The␈αtypical␈αLISP␈αtrick␈αis␈αto␈αuse␈αthe␈αatoms␈α␈↓αT␈↓␈αand␈α␈↓αNIL␈↓␈αrather␈αthan␈α␈↓
t␈↓␈αand␈α␈↓
f␈↓
␈↓ ↓H␈↓as␈α∀truth␈α∪values.␈α∀ Our␈α∪heresy␈α∀will␈α∪disallow␈α∀some␈α∪mixed␈α∀compositions␈α∪of␈α∀LISP␈α∀functions␈α∪and
␈↓ ↓H␈↓predicates.␈α We␈αshall␈αnot␈αapologize␈αfor␈αthat␈αand␈αby␈αthe␈αend␈αof␈αthis␈αchapter␈αyou␈αshould␈αsee␈αwhy␈αwe
␈↓ ↓H␈↓have deviated.
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     23␈↓


␈↓ ↓H␈↓α␈↓ ∧qeq[A;A] = ␈↓
t␈↓α                eq[A;B] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ∧&eq[(A . B); A] = ␈↓λB␈↓α       eq[(A . B);(A . B)] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ∧ueq[eq[A;B];D] = ␈↓λB␈↓α     eq[␈↓λB␈↓α;x] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ∧Peq[car[(A . B)];car[cdr[(A .(B . C))]]] = ␈↓
f␈↓

␈↓ ↓H␈↓For␈α
completeness'␈α
sake␈α
we␈α
should␈α
define␈α
a␈α
version␈α
of␈α
␈↓αeq␈↓,␈α
say␈α
␈↓αeq␈↓βTr␈↓,␈α
which␈α
is␈α
defined␈α
over␈α∞␈↓	Tr␈↓␈α
and
␈↓ ↓H␈↓acts␈α
like␈α␈↓αeq␈↓.␈α
For␈αexpediency's␈α
sake␈α
we␈αwill␈α
simply␈αextend␈α
the␈αdefinition␈α
of␈α
␈↓αeq␈↓␈αto␈α
␈↓	S␈↓β1␈↓␈αso␈α
that␈α
it␈αmay
␈↓ ↓H␈↓compare two elements of ␈↓	Tr␈↓.
␈↓ ↓H␈↓α␈↓ ∧veq[␈↓
t␈↓α;␈↓
t␈↓α] = ␈↓
t␈↓α                eq[␈↓
f␈↓α;␈↓λB␈↓α] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ¬βeq[␈↓
f␈↓α;␈↓
f␈↓α] = ␈↓
t␈↓α                eq[␈↓
t␈↓α;␈↓
f␈↓α] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ εεeq[A;␈↓
t␈↓α] = ␈↓λB␈↓

␈↓ ↓H␈↓We␈α∪need␈α∩to␈α∪include␈α∪a␈α∩construct␈α∪in␈α∪our␈α∩language␈α∪to␈α∪effect␈α∩a␈α∪test-and-branch␈α∪operation.␈α∩ The
␈↓ ↓H␈↓IF-THEN-ELSE operation in LISP is called the conditional expression.  It is written:

␈↓ ↓H␈↓α␈↓ ¬
[p␈↓β1␈↓α → e␈↓β1␈↓α; p␈↓β2␈↓α → e␈↓β2␈↓α; ... ; p␈↓βn␈↓α  → e␈↓βn␈↓α]

␈↓ ↓H␈↓Each␈α⊂␈↓αp␈↓βi␈↓␈α⊂is␈α⊂a␈α⊂predicate␈α⊂and␈α⊂therefore␈α⊂takes␈α⊂on␈α⊂values␈α⊂in␈α⊂the␈α⊂set␈α⊂␈↓	Tr␈↓␈α⊂or␈α⊂gives␈α⊂␈↓λB␈↓;␈α⊂each␈α⊂␈↓αe␈↓βi␈↓␈α⊂is␈α⊂an
␈↓ ↓H␈↓expression␈αwhich␈αwill␈αgive␈αa␈αvalue␈αin␈α␈↓	S␈↓β1␈↓.␈αWe␈αwill␈αrestrict␈αthe␈αconditionals␈αsuch␈αthat␈αall␈αthe␈α␈↓αe␈↓βi␈↓␈αmust
␈↓ ↓H␈↓have values in the ␈↓↓same␈↓ domain or be ␈↓λB␈↓; i.e. all be in ␈↓<sexpr>␈↓ or all be in ␈↓	Tr␈↓.

␈↓ ↓H␈↓The meaning (or semantics) of conditionals is:

␈↓ ↓H␈↓␈↓ αhWe␈αevaluate␈αthe␈α␈↓αp␈↓βi␈↓'s␈αfrom␈αleft␈αto␈αright,␈αfinding␈αthe␈α␈↓↓first␈↓␈αwhich␈αreturns␈αvalue␈α␈↓
t␈↓.␈α When
␈↓ ↓H␈↓␈↓ αhwe␈α⊂find␈α∂such␈α⊂a␈α∂␈↓αp␈↓βi␈↓,␈α⊂we␈α∂evaluate␈α⊂the␈α∂corresponding␈α⊂␈↓αe␈↓βi␈↓.␈α∂ The␈α⊂value␈α∂of␈α⊂the␈α∂conditional
␈↓ ↓H␈↓␈↓ αhexpression␈α∞is␈α∂the␈α∞value␈α∂computed␈α∞by␈α∂that␈α∞␈↓αe␈↓βi␈↓;␈α∂if␈α∞all␈α∂of␈α∞the␈α∂␈↓αp␈↓βi␈↓'s␈α∞evaluate␈α∂to␈α∞␈↓
f␈↓␈α∂then␈α∞the
␈↓ ↓H␈↓␈↓ αhconditional␈α∞expression␈α∞gives␈α∞␈↓λB␈↓.␈α
 The␈α∞conditional␈α∞expression␈α∞also␈α
gives␈α∞␈↓λB␈↓␈α∞if␈α∞we␈α
come
␈↓ ↓H␈↓␈↓ αhacross a ␈↓αp␈↓βi␈↓ which has value ␈↓λB␈↓ before we hit a ␈↓αp␈↓βi␈↓ with value ␈↓
t␈↓.
␈↓ ↓H␈↓Examples:
␈↓ ↓H␈↓α␈↓ ∧X[atom [A] → B; eq [A;(A . B)] → C] = B
␈↓ ↓H␈↓α␈↓ ∧V[eq [A;(A . B)] → C; atom [A] → B] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ αo[atom [(A . B)] → B; eq [A ; B] → C; eq [car[(A . B)]; cdr[(B . A)]] → E] = E
␈↓ ↓H␈↓α␈↓ ¬α[eq [A; A] → ␈↓
t␈↓α; atom [A] → ␈↓
f␈↓α] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ∧w[eq [A; A] → ␈↓
t␈↓α; atom [A] → B] = ␈↓λB␈↓α

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αp␈↓β2␈↓␈αexpression␈α
of␈αthe␈αfirst␈αexample␈αis␈α
undefined,␈αbut␈αthe␈αconditional␈αgives␈α
value␈α␈↓αB␈↓
␈↓ ↓H␈↓since␈α→p␈↓β1␈↓␈α→gives␈α→value␈α_␈↓
t␈↓.␈α→ Thus␈α→conditional␈α→expressions␈α_are␈α→non-strict.␈α→Note␈α→however␈α_that
␈↓ ↓H␈↓non-strictness␈αis␈αrelative␈α
to␈αa␈αsingle␈α
domain;␈αthus␈αthe␈α
last␈αexample␈αabove␈α
gives␈α␈↓λB␈↓␈αsince␈α
it␈αcontains
␈↓ ↓H␈↓␈↓αe␈↓βi␈↓'s of differing domains.

␈↓ ↓H␈↓Frequently␈αit␈αis␈αconvenient␈αto␈α
use␈αa␈αspecial␈αform␈αof␈αthe␈α
conditional␈αexpression␈αwhere␈αthe␈αfinal␈α␈↓αp␈↓βn␈↓␈α
is
␈↓ ↓H␈↓guaranteed␈α≥to␈α≤be␈α≥true.␈α≤You␈α≥can␈α≤think␈α≥of␈α≤lots␈α≥of␈α≤predicates␈α≥which␈α≤are␈α≥always␈α≤true
␈↓ ↓H␈↓(for example, ␈↓αeq[1;1]␈↓).  A natural predicate is the constant predicate, ␈↓
t␈↓; the value of ␈↓
t␈↓ is always ␈↓
t␈↓.
␈↓ ↓H␈↓␈↓↓24  Symbolic expressions␈↓ 42.5␈↓



␈↓ ↓H␈↓Thus the special form:

␈↓ ↓H␈↓α␈↓ ¬Q[p␈↓β1␈↓α  → e␈↓β1␈↓α; ...; ␈↓
t␈↓α → e␈↓βn␈↓α]

␈↓ ↓H␈↓Now␈αif␈α
we␈αknow␈α
that␈αthe␈α
previous␈α␈↓αp␈↓βi␈↓'s␈αare␈α
either␈αtrue␈α
or␈αfalse␈↓π 16␈↓,␈α
the␈αfinal␈α
␈↓αp␈↓βn␈↓ → ␈↓αe␈↓βn␈↓-case␈αis␈αa␈α
catch-all
␈↓ ↓H␈↓or␈αotherwise-case␈αwhich␈αwill␈αbe␈αexecuted␈αif␈αnone␈αof␈α
the␈αprevious␈α␈↓αp␈↓βi␈↓'s␈αgive␈α␈↓
t␈↓.␈α Thus␈αthe␈αuse␈αof␈α
␈↓
t␈↓␈αin
␈↓ ↓H␈↓this context can be read "otherwise"; and the conditional can be read:

␈↓ ↓H␈↓␈↓ βN"If ␈↓αp␈↓β1␈↓ is true then ␈↓αe␈↓β1␈↓, else if ␈↓αp␈↓β2␈↓ is true then ..., otherwise ␈↓αe␈↓βn␈↓."

␈↓ ↓H␈↓The␈α∩introduction␈α∩of␈α∪conditional␈α∩expressions␈α∩has␈α∩further␈α∪widened␈α∩the␈α∩gap␈α∪between␈α∩traditional
␈↓ ↓H␈↓mathematical␈αtheories␈αand␈αcomputational␈αtheories.␈αPreviously␈αwe␈αcould␈αalmost␈αside-step␈αthe␈αissue␈α
of
␈↓ ↓H␈↓order␈αof␈αevaluation;␈αit␈α
didn't␈αreally␈αmatter␈αunless␈α␈↓λB␈↓␈α
got␈αinto␈αthe␈αact.␈α
But␈αnow␈αthe␈αvery␈αdefinition␈α
of
␈↓ ↓H␈↓meaning of conditionals involves an order of evaluation.

␈↓ ↓H␈↓First,␈α⊂the␈α⊂order␈α⊂of␈α⊂evaluation␈α⊂is␈α⊃important␈α⊂from␈α⊂a␈α⊂computational␈α⊂viewpoint␈α⊂on␈α⊂the␈α⊃grounds␈α⊂of
␈↓ ↓H␈↓efficiency:␈αif␈αwe␈αare␈αgoing␈αto␈αgive␈αas␈αvalue␈αthe␈αleftmost␈α␈↓αe␈↓βi␈↓␈αwhose␈α␈↓αp␈↓βi␈↓␈αevaluates␈αto␈α␈↓
t␈↓,␈αthen␈αthere␈αis␈αno
␈↓ ↓H␈↓need␈α
to␈αcompute␈α
any␈α
of␈αthe␈α
other␈α
␈↓αe␈↓βj␈↓'s;␈αthose␈α
values␈α
will␈αnever␈α
be␈α
used.␈α A␈α
more␈α
pressing␈αdifficulty␈α
is
␈↓ ↓H␈↓that␈α∂of␈α∂partial␈α∂functions.␈α∂If␈α∂we␈α∂did␈α∂not␈α∂impose␈α∂an␈α∂order␈α∂of␈α∂evaluation␈α∂on␈α∂the␈α∂components␈α⊂of␈α∂a
␈↓ ↓H␈↓conditional,␈α∀then␈α∀frequently␈α∀we␈α∀would␈α∀attempt␈α∀to␈α∀evaluate␈α∀expressions␈α∀which␈α∀would␈α∃lead␈α∀to
␈↓ ↓H␈↓undefined␈α
results:␈α∞␈↓α[eq[0;0] → 1; ␈↓
t␈↓α → car[A]]␈↓␈α
gives␈α
␈↓α1␈↓␈α∞using␈α
the␈α
meaning␈α∞of␈α
conditionals,␈α∞whereas␈α
the
␈↓ ↓H␈↓expression␈α
would␈α
be␈α
undefined␈α
if␈α
we␈α
were␈α
forced␈α
to␈α
evaluate␈α
␈↓αcar[A]␈↓.␈α
This␈α
would␈α
cause␈α
us␈α
to␈α
lose
␈↓ ↓H␈↓unnecessarily␈α⊂if␈α⊃we␈α⊂think␈α⊂of␈α⊃an␈α⊂occurrence␈α⊂of␈α⊃␈↓λB␈↓␈α⊂during␈α⊂evaluation␈α⊃being␈α⊂mapped␈α⊂to␈α⊃an␈α⊂error
␈↓ ↓H␈↓message,␈α∩and␈α⊃causing␈α∩termination␈α⊃of␈α∩the␈α⊃computation.␈α∩ But,␈α⊃if␈α∩we␈α⊃continue␈α∩to␈α⊃allow␈α∩␈↓λB␈↓␈α∩as␈α⊃an
␈↓ ↓H␈↓argument␈αor␈αvalue␈αto␈αa␈αfunction,␈αthen␈αwe␈αcan␈αcharacterize␈αthe␈αeffect␈αof␈αa␈αconditional␈αexpression␈αas
␈↓ ↓H␈↓a␈α␈↓↓non-strict␈↓␈αfunction.␈α Recall,␈αa␈αnon-strict␈αfunction␈αis␈αallowed␈αto␈αreturn␈αa␈αvalue␈αother␈αthan␈α␈↓λB␈↓␈αwhen
␈↓ ↓H␈↓one␈α∞of␈α∞its␈α∂arguments␈α∞is␈α∞␈↓λB␈↓;␈α∞or,␈α∂put␈α∞another␈α∞way,␈α∞we␈α∂don't␈α∞examine␈α∞the␈α∞definedness␈α∂of␈α∞arguments
␈↓ ↓H␈↓before applying the function.

␈↓ ↓H␈↓For␈α∂example,␈α⊂let␈α∂␈↓αif(x;y;z)␈↓␈α∂be␈α⊂the␈α∂␈↓↓conditional␈α⊂function␈↓␈α∂␈↓π 17␈↓␈α∂computed␈α⊂by:␈α∂␈↓α[x → y; ␈↓
t␈↓α → z]␈↓.␈α⊂ We␈α∂can
␈↓ ↓H␈↓define ␈↓αif␈↓ as a non-strict function such that:
␈↓ ↓H␈↓␈↓ βx␈↓ ¬h␈↓αy␈↓ if ␈↓αx␈↓ is ␈↓
t␈↓
␈↓ ↓H␈↓␈↓ βx␈↓αif(x;y;z) =␈↓ ¬hz␈↓ if ␈↓αx␈↓ is ␈↓
f␈↓
␈↓ ↓H␈↓␈↓ βx␈↓ ¬h␈↓λB␈↓ if ␈↓αx␈↓ is ␈↓λB␈↓

␈↓ ↓H␈↓However␈αthere␈αis␈αmore␈αto␈αthe␈α"strictness"␈αimplied␈αby␈αconditional␈αexpressions␈αthan␈αjust␈αmaking␈αsure
␈↓ ↓H␈↓that proper arguments are passed on function calls.

␈↓ ↓H␈↓Consider the following algorithm:
␈↓ ↓H␈↓α␈↓ ¬_f[x] <= [x=0 → 1; ␈↓
t␈↓α  → f[x-1]]
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 16␈↓ and we know that all the ␈↓αe␈↓βi␈↓'s are elements of the same domain.

␈↓ ↓H␈↓␈↓π 17␈↓␈αNotice␈αwe␈αare␈αwriting␈α`(...)'␈αrather␈αthan␈α`[...]'␈αsince␈αwe␈αare␈αtalking␈αabout␈αthe␈αfunction␈αand␈αnot␈αthe
␈↓ ↓H␈↓algorithm. See page 17.
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     25␈↓


␈↓ ↓H␈↓Assume␈α
the␈α
domain␈α
of␈αdiscourse␈α
is␈α
the␈α
integers;␈α
and␈αassume␈α
that␈α
␈↓αf␈↓␈α
is␈α
non-strict.␈αThat␈α
is,␈α
␈↓αf␈↓␈α
will␈αtry␈α
to
␈↓ ↓H␈↓compute␈α
with␈α
␈↓↓any␈↓␈α
(integer)␈α∞argument␈α
it␈α
is␈α
given.␈α
 This␈α∞algorithm␈α
defines␈α
a␈α
function␈α
giving␈α∞␈↓α1␈↓␈α
for
␈↓ ↓H␈↓any␈α
non-negative␈α
integer␈α
and␈α
is␈α
undefined␈α
for␈αany␈α
other␈α
number.␈α
From␈α
a␈α
computational␈α
point␈αof
␈↓ ↓H␈↓view,␈α
however,␈α
␈↓αf[-1]␈↓␈α
appears␈α
"undefined"␈α
in␈αa␈α
different␈α
sense␈α
from␈α
␈↓αcar[A]␈↓␈α
being␈α
"undefined".␈αThe
␈↓ ↓H␈↓computation␈α␈↓αf[-1]␈↓␈αdoes␈αnot␈αterminate␈αand␈αis␈αsaid␈αto␈α␈↓↓diverge␈↓.␈α For␈αa␈αpartial␈αfunction␈αlike␈α␈↓αcar␈↓,␈αwe␈αcan
␈↓ ↓H␈↓conceive␈α⊂of␈α⊃giving␈α⊂an␈α⊃error␈α⊂message␈α⊂whenever␈α⊃we␈α⊂attempt␈α⊃to␈α⊂apply␈α⊂the␈α⊃function␈α⊂to␈α⊃an␈α⊂atomic
␈↓ ↓H␈↓argument.␈α
But␈αit␈α
stretches␈αone's␈α
credulity␈αto␈α
expect␈α
to␈αinclude␈α
tests␈αlike␈α
"if␈αthe␈α
computation␈α␈↓αf[a]␈↓␈α
does
␈↓ ↓H␈↓not␈αterminate␈αthen␈αgive␈αerror␈αNo.␈α15."␈↓π 18␈↓.␈α From␈αthe␈αpurely␈αfunctional␈αpoint␈αof␈αview,␈α␈↓αf␈↓␈αstill␈αdefines
␈↓ ↓H␈↓the␈α∞partial␈α∞function␈α∂which␈α∞is␈α∞␈↓α1␈↓␈α∞for␈α∂the␈α∞non-negative␈α∞integers,␈α∞regardless␈α∂of␈α∞how␈α∞badly␈α∂it␈α∞botches
␈↓ ↓H␈↓things up for negative numbers.

␈↓ ↓H␈↓So␈α
a␈α
computation␈αmay␈α
be␈α
"undefined"␈αfor␈α
two␈α
reasons:␈αit␈α
involves␈α
a␈α
non-terminating␈αcomputation
␈↓ ↓H␈↓or␈α
it␈α
involves␈α
applying␈α
a␈α
partial␈α
function␈α
to␈α
a␈α
value␈α
not␈α
in␈α
its␈α
domain.␈α
 Note␈α
that␈α
the␈α
distinction
␈↓ ↓H␈↓between␈α
"undefined"␈αand␈α
"diverges"␈α
is␈αfuzzy.␈α
If␈α
we␈αrestrict␈α
the␈α
domain␈αof␈α
the␈α
above␈α␈↓αf␈↓␈α
to␈αthe␈α
natural
␈↓ ↓H␈↓numbers,␈α
then␈α␈↓αf[-1]␈↓␈α
denotes␈α
␈↓λB␈↓␈αrather␈α
than␈α
diverges.␈αOr,␈α
put␈α
another␈αway,␈α
"undefined␈α
strictness"␈αis␈α
a
␈↓ ↓H␈↓special␈α⊂case␈α⊂of␈α⊂"divergent␈α⊂strictness"␈α⊂where␈α⊂we␈α⊂are␈α⊂able␈α⊂to␈α⊂predict␈α⊂which␈α⊂computations␈α⊂will␈α⊂not
␈↓ ↓H␈↓terminate.␈α
Those␈α
cases␈αcan␈α
be␈α
checked␈αby␈α
defining␈α
the␈αfunction␈α
to␈α
be␈αstrict␈α
over␈α
a␈α
domain␈αwhich
␈↓ ↓H␈↓rules␈α
out␈α
those␈α
anomalies.␈α
 Thus␈α
a␈α
case␈α
can␈α
be␈α
made␈α
for␈α
identifying␈α
divergent␈α
computations␈α
with␈α
␈↓λB␈↓;
␈↓ ↓H␈↓however there is typically more to non-termination that just "wrong kind of arguments applied".

␈↓ ↓H␈↓We␈α
want␈α
to␈α
extend␈α∞our␈α
discussion␈α
of␈α
strictness␈α
to␈α∞encompass␈α
divergence.␈α
Recall␈α
the␈α∞discussion␈α
on
␈↓ ↓H␈↓page 19 of
␈↓ ↓H␈↓α␈↓ ¬bsecond[x; y] <= y.

␈↓ ↓H␈↓Defining␈α⊂␈↓αsecond␈↓␈α⊃to␈α⊂be␈α⊂strict␈α⊃required␈α⊂that␈α⊃each␈α⊂application␈α⊂of␈α⊃␈↓αsecond␈↓␈α⊂determine␈α⊃whether␈α⊂either
␈↓ ↓H␈↓argument␈αdenoted␈α␈↓λB␈↓.␈α If␈αwe␈αwant␈α␈↓αfoo␈↓␈αto␈αbe␈αstrict␈αwith␈αrespect␈αto␈αdivergence,␈αthen␈αwe␈αmust␈αtest␈αeach
␈↓ ↓H␈↓argument␈α∂for␈α∂divergence.␈α∂That␈α⊂implies␈α∂evaluation␈α∂of␈α∂the␈α∂each␈α⊂of␈α∂the␈α∂arguments,␈α∂which␈α⊂in␈α∂turn
␈↓ ↓H␈↓implies␈α⊃that␈α⊃if␈α⊃a␈α⊃computation␈α⊃of␈α⊃an␈α⊃argument␈α⊃diverges,␈α⊃then␈α⊃the␈α⊃computation␈α⊃of␈α⊃the␈α⊃function
␈↓ ↓H␈↓application␈α∂must␈α∂also␈α∂diverge.␈α∂ This␈α∞implies␈α∂that␈α∂it␈α∂is␈α∂natural␈α∞to␈α∂associate␈α∂"strict␈α∂with␈α∂respect␈α∞to
␈↓ ↓H␈↓divergence"␈α
with␈α␈↓	CBV␈↓,␈α
since␈αin␈α
the␈α
process␈αof␈α
checking␈αfor␈α
termination,␈αwe␈α
must␈α
compute␈αvalues.
␈↓ ↓H␈↓However we already know that if a function is strict then calling style doesn't matter.

␈↓ ↓H␈↓In␈α∀contrast,␈α∀a␈α∀non-strict␈α∀function␈α∀does␈α∃not␈α∀check␈α∀arguments␈α∀for␈α∀divergence,␈α∀and␈α∃indeed␈α∀the
␈↓ ↓H␈↓divergence␈α∃of␈α∀a␈α∃computation␈α∀may␈α∃depend␈α∀on␈α∃the␈α∀calling␈α∃style.␈α∀ Consider␈α∃the␈α∃evaluation␈α∀of
␈↓ ↓H␈↓␈↓αsecond[f[-1]; B]␈↓␈α⊂where␈α⊂␈↓αf␈↓␈α⊂was␈α⊂defined␈α⊂above␈α⊂and␈α⊂is␈α⊂total␈α⊂over␈α⊂the␈α⊂integers.␈α⊂This␈α⊃evaluation␈α⊂will
␈↓ ↓H␈↓diverge␈αunder␈α␈↓	CBV␈↓␈α
while␈αit␈αconverges␈α
to␈α␈↓αB␈↓␈αusing␈α␈↓	CBN␈↓.␈α
 We␈αcannot␈αrestrict␈α
all␈αour␈αfunctions␈αto␈α
be
␈↓ ↓H␈↓strict␈α⊂if␈α⊂we␈α⊂expect␈α⊂to␈α⊂do␈α⊂any␈α⊂non-trivial␈α⊂computation.␈α⊂ That␈α⊂is,␈α⊂we␈α⊂need␈α⊂a␈α⊂function␈α⊂which␈α⊂can
␈↓ ↓H␈↓determine␈αits␈αvalue␈αwithout␈αcomplete␈αinformation␈α
concerning␈αthe␈αvalues␈αof␈αall␈αof␈αits␈α
arguments␈α--a
␈↓ ↓H␈↓"don't␈α
care␈αcondition"--.␈α
 If␈αall␈α
the␈αarguments␈α
to␈α
all␈αsubfunctions␈α
must␈αbe␈α
evaluated␈αthe␈α
computation
␈↓ ↓H␈↓will not terminate.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 18␈↓␈α∞Indeed,␈α
there␈α∞are␈α
good␈α∞reasons␈α
to␈α∞be␈α∞sceptical␈α
about␈α∞the␈α
existence␈α∞of␈α
such␈α∞omniscient␈α∞tests.␈α
A
␈↓ ↓H␈↓discussion␈αof␈αsuch␈αtopics␈αinvolves␈αa␈αdescription␈αof␈αthe␈α"halting␈αproblem"␈αfor␈αcomputational␈αdevices.
␈↓ ↓H␈↓See [Rog 67] for details.
␈↓ ↓H␈↓␈↓↓26  Symbolic expressions␈↓ 42.5␈↓


␈↓ ↓H␈↓The␈α∩conditional␈α∩function␈α∩is␈α⊃such␈α∩a␈α∩non-strict␈α∩function.␈α∩ That␈α⊃is␈α∩␈↓αif(␈↓
t␈↓α;q;r)␈↓␈α∩has␈α∩value␈α∩␈↓αq␈↓␈α⊃without
␈↓ ↓H␈↓knowing␈α∂anything␈α∂about␈α∂what␈α∂happens␈α∂to␈α∂␈↓αr␈↓.␈α∂ In␈α∂particular,␈α∂␈↓αif(␈↓
t␈↓α;q;␈↓λB␈↓α) = q␈↓.␈α∂ Likewise␈α∞␈↓αif(␈↓
f␈↓α;␈↓λB␈↓α;r) = r␈↓.
␈↓ ↓H␈↓Now␈αsince␈α␈↓αif␈↓␈αis␈αto␈αbe␈αa␈αfunction␈α
and␈αtherefore␈αsingle-valued,␈αif␈α␈↓αif(␈↓
t␈↓α;q;␈↓λB␈↓α) = q␈↓␈αthen␈αfor␈αany␈α
argument
␈↓ ↓H␈↓␈↓αx␈↓, ␈↓αif(␈↓
t␈↓α;q;x) = q␈↓.  Thus ␈↓αif(␈↓
t␈↓α;x;y)␈↓ and ␈↓αif(␈↓
f␈↓α;y;x)␈↓ act like functions of the form:
␈↓ ↓H␈↓α␈↓ βof(x;␈↓λB␈↓α) = z ␈↓ implies for ␈↓↓every␈↓ ␈↓αy␈↓ in the domain ␈↓αf(x;y)=z␈↓.

␈↓ ↓H␈↓Such␈αfunctions␈αare␈αsaid␈αto␈αbe␈α␈↓↓monotonic␈αfunctions␈↓.␈α Notice␈αthat␈α␈↓λB␈↓␈αis␈αnow␈αcarrying␈α
an␈αadditional
␈↓ ↓H␈↓"don't-care"␈αinterpretation,␈α
certainly␈αconsistent␈αwith␈α
its␈αprevious␈αassignments␈α
when␈αwe␈αthink␈α
of␈αthe
␈↓ ↓H␈↓function being computed by the algorithm.

␈↓ ↓H␈↓Even␈α
given␈α
that␈α
a␈αcomputational␈α
definition␈α
is␈α
desired,␈αthere␈α
are␈α
other␈α
plausible␈α
interpretations␈αof
␈↓ ↓H␈↓conditionals.␈α∂ Consider␈α∂the␈α∂nonsense␈α∞definition:␈α∂␈↓αg[x;y] <= [lic[x] → 1;␈↓
t␈↓α → 1]␈↓.␈α∂Clearly,␈α∂assuming␈α∞that
␈↓ ↓H␈↓␈↓αlic␈↓␈α∞is␈α∞a␈α∞total␈α∞predicate,␈α∞any␈α∞value␈α∞computed␈α
by␈α∞␈↓αg␈↓␈α∞will␈α∞be␈α∞␈↓α1␈↓.␈α∞But␈α∞requiring␈α∞left-to-right␈α
evaluation
␈↓ ↓H␈↓could␈α∀spend␈α∀a␈α∀great␈α∃deal␈α∀of␈α∀unnecessary␈α∀computation␈α∀if␈α∃␈↓αlic␈↓␈α∀is␈α∀a␈α∀␈↓l␈↓ong␈α∃␈↓i␈↓nvolved␈α∀␈↓c␈↓alculation.
␈↓ ↓H␈↓Questions␈α∂of␈α∂evaluation␈α∂are␈α∂non-trivial.␈α∂You␈α∂should␈α∞at␈α∂least␈α∂be␈α∂aware␈α∂that␈α∂some␈α∂decisions␈α∞have
␈↓ ↓H␈↓been made and others were possible.

␈↓ ↓H␈↓What␈α⊂benefits␈α⊂have␈α∂resulted␈α⊂from␈α⊂our␈α∂study␈α⊂of␈α⊂␈↓λB␈↓␈α∂and␈α⊂divergence?␈α⊂ We␈α∂should␈α⊂have␈α⊂a␈α∂clearer
␈↓ ↓H␈↓understanding␈αof␈αthe␈αdifference␈αbetween␈αfunction␈αand␈α
algorithm␈αand␈αa␈αbetter␈αgrasp␈αof␈αthe␈αkinds␈α
of
␈↓ ↓H␈↓difficulties␈α∞which␈α∞can␈α∞befall␈α∞an␈α∞unwary␈α∞computation.␈α∞ We␈α∞have␈α∞uncovered␈α∞an␈α∞important␈α∞class␈α
of
␈↓ ↓H␈↓detectable␈αerrors.␈α The␈αcharacter␈αof␈αthese␈αmiscreants␈α
is␈αthat␈αthey␈αoccur␈αin␈αthe␈αcontext␈α
of␈αsupplying
␈↓ ↓H␈↓the␈αwrong␈α␈↓↓kind␈↓␈αof␈αargument␈αto␈αa␈αfunction.␈αThis␈αkind␈αof␈αerror␈αis␈αcalled␈αa␈α␈↓↓type␈αfault␈↓,␈αmeaning␈αthat
␈↓ ↓H␈↓we␈α
expected␈α∞an␈α
argument␈α
of␈α∞a␈α
specific␈α∞type,␈α
that␈α
is␈α∞from␈α
a␈α∞specific␈α
domain,␈α
and␈α∞since␈α
it␈α∞was␈α
not
␈↓ ↓H␈↓forthcoming,␈α∀we␈α∀refuse␈α∀to␈α∀perform␈α∀any␈α∀kind␈α∀of␈α∀calculation.␈α∀Thus␈α∀␈↓αatom[␈↓
f␈↓α]␈↓␈α∀and␈α∀␈↓αcons[␈↓
t␈↓α;A]␈↓␈α∀are
␈↓ ↓H␈↓undefined␈α∞since␈α∞both␈α∞expect␈α∞elements␈α∞of␈α∂␈↓	S␈↓␈α∞as␈α∞arguments.␈α∞See␈α∞page 178␈α∞for␈α∞further␈α∂discussion␈α∞of
␈↓ ↓H␈↓type␈α⊂faults.␈α⊂ Divergent␈α⊂computations␈α⊂are␈α⊂equally␈α⊂repugnant␈α⊂but␈α⊂there␈α⊂is␈α⊂no␈α⊂general␈α⊂method␈α∂for
␈↓ ↓H␈↓testing whether an arbitrary calculation will terminate.

␈↓ ↓H␈↓It␈α∩may␈α∩not␈α⊃seem␈α∩like␈α∩you␈α⊃can␈α∩do␈α∩much␈α⊃useful␈α∩computation␈α∩with␈α⊃such␈α∩a␈α∩limited␈α∩collection␈α⊃of
␈↓ ↓H␈↓operations␈α⊂as␈α∂those␈α⊂proposed␈α⊂in␈α∂LISP.␈α⊂Things␈α⊂are␈α∂not␈α⊂quite␈α∂as␈α⊂trivial␈α⊂as␈α∂they␈α⊂might␈α⊂seem.␈α∂ In
␈↓ ↓H␈↓elementary␈αnumber␈αtheory␈αall␈αyou␈αhave␈αis␈αzero␈αand␈αsome␈αsimple␈αfunctions,␈αand␈αelementary␈αnumber
␈↓ ↓H␈↓theory␈α
is␈αfar␈α
from␈αelementary.␈α
 Manipulation␈αof␈α
our␈αprimitives,␈α
with␈αcomposition,␈α
and␈αconditional
␈↓ ↓H␈↓expressions, coupled with techniques for definition can ␈↓↓also␈↓ become complicated.

␈↓ ↓H␈↓Let's␈α∩apply␈α∩the␈α∩LISP␈α∩constructs␈α∩which␈α∩we␈α⊃now␈α∩have,␈α∩and␈α∩define␈α∩a␈α∩new␈α∩LISP␈α∩function.␈α⊃ For
␈↓ ↓H␈↓example:␈α
our␈αpredicate␈α
␈↓αeq␈↓␈αis␈α
defined␈α
only␈αfor␈α
atomic␈αarguments.␈α
 We␈α
would␈αlike␈α
to␈αbe␈α
able␈α
to␈αtest
␈↓ ↓H␈↓for␈αequality␈αof␈αarbitrary␈αS-exprs.␈α What␈αshould␈αthis␈αmore␈αcomplex␈αequality␈αmean?␈α By␈αequality␈αwe
␈↓ ↓H␈↓mean:␈α∂as␈α⊂trees,␈α∂the␈α⊂S-exprs␈α∂have␈α⊂the␈α∂same␈α⊂branching␈α∂structure;␈α⊂and␈α∂the␈α⊂corresponding␈α∂terminal
␈↓ ↓H␈↓nodes are labeled by the same atoms.  Thus, we would like to define a predicate, ␈↓αequal␈↓, such that:

␈↓ ↓H␈↓α␈↓ ∧Oequal [(A . B);(A . B)] = ␈↓
t␈↓α = equal [A;A]
␈↓ ↓H␈↓α␈↓ ¬+equal [(A . B);(B . A)] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ ∧hequal [(A . (B . C));(A . (B . C))] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ∧hequal [(A . (B . C));((A . B) . C)] = ␈↓
f␈↓α
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     27␈↓


␈↓ ↓H␈↓Here's an intuitive description of such a predicate named ␈↓αequal␈↓.

␈↓ ↓H␈↓1.␈α If␈αboth␈αarguments␈α
are␈αatomic␈αthen␈αsee␈αwhat␈α
␈↓αeq␈↓␈αsays␈αabout␈αthem␈α(are␈α
they␈α"␈↓αeq␈↓").␈α We␈αcan␈α
test␈αif
␈↓ ↓H␈↓␈↓ αλthey are both atomic by using ␈↓αatom␈↓ and a conditional expression.

␈↓ ↓H␈↓2.  If one is atomic and the other is not they can't be equal S-exprs.

␈↓ ↓H␈↓3.␈α∂ Otherwise␈α∂both␈α∞are␈α∂non-atomic␈α∂S-exprs.␈α∞ Both␈α∂have␈α∂two␈α∞sub-expressions.␈α∂ Look␈α∂at␈α∂both␈α∞first
␈↓ ↓H␈↓␈↓ α_subexpressions.␈α_ If␈α_the␈α_first␈α_sub-expressions␈α↔are␈α_not␈α_equal␈α_then␈α_certainly␈α_the␈α↔initial
␈↓ ↓H␈↓␈↓ α_expressions␈αcannot␈α
hope␈αto␈αbe␈α
equal.␈α If,␈αhowever,␈α
the␈αfirst␈αsubexpressions␈α
are␈αequal␈αthen␈α
the
␈↓ ↓H␈↓␈↓ α_question␈α⊂of␈α⊂whether␈α⊂or␈α⊂not␈α⊂the␈α⊂initial␈α⊂expressions␈α⊂are␈α⊂equal␈α⊂depends␈α⊂on␈α⊂the␈α⊂equality␈α∂(or
␈↓ ↓H␈↓␈↓ α_non-equality) of the second subexpressions.  Thus the following definition:

␈↓ ↓H␈↓α     equal[x;y] <=␈↓ βh[atom[x] → [atom[y] → eq [x;y]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βh atom[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βh equal [car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓ ↓H␈↓α␈↓ βh ␈↓
t␈↓α → ␈↓
f␈↓α]


␈↓ ↓H␈↓α␈↓Notice␈αthat␈αwe␈αuse␈αnested␈α
conditional␈αexpressions␈αin␈α␈↓αequal␈↓:␈αe␈↓β1␈↓␈α
is␈αitself␈αa␈αconditional.␈αAlso␈α
we␈αhave
␈↓ ↓H␈↓used␈α⊃predicates␈α⊃in␈α∩the␈α⊃e␈↓βi␈↓␈α⊃positions␈α∩at␈α⊃e␈↓β3␈↓␈α⊃and␈α⊃e␈↓β11␈↓;␈α∩this␈α⊃is␈α⊃perfectly␈α∩reasonable␈α⊃since␈α⊃␈↓αequal␈↓␈α∩is␈α⊃a
␈↓ ↓H␈↓predicate and thus returns either ␈↓
f␈↓ or ␈↓
t␈↓.

␈↓ ↓H␈↓Let's␈α∪show␈α∪that␈α∪␈↓αequal␈↓␈α∪does␈α∩perform␈α∪correctly␈α∪for␈α∪a␈α∪specific␈α∩example.␈α∪ This␈α∪will␈α∪also␈α∪show␈α∩a
␈↓ ↓H␈↓complicated evaluation of a conditional expression.

␈↓ ↓H␈↓αequal[(A . B);(A . C)] =␈↓ βx[atom[(A . B)] → [atom[(A . C)] → eq [(A . B);(A . C)]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βx atom[(A . C)] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βx equal [car[(A . B)];car[(A . C)]] → equal[cdr[(A . B)];cdr[(A . C)]];
␈↓ ↓H␈↓α␈↓ βx ␈↓
t␈↓α → ␈↓
f␈↓α]

␈↓ ↓H␈↓Now␈α⊃using␈α⊃the␈α⊃meaning␈α⊃of␈α⊃conditionals␈α⊃(page 23),␈α⊃we␈α⊃find␈α⊃that␈α⊃p␈↓β1␈↓␈α⊃(i.e., ␈↓αatom[(A . B)]␈↓ )␈α⊃and␈α⊂p␈↓β2␈↓
␈↓ ↓H␈↓( ␈↓αatom[(A . C)]␈↓ )␈α%when␈α%evaluated␈α$(in␈α%order)␈α%give␈α%␈↓
f␈↓.␈α$ We␈α%must␈α%now␈α%evaluate␈α$p␈↓β3␈↓:
␈↓ ↓H␈↓␈↓αequal[car[(A . B)];car[(A . C)]]␈↓.  This reduces to ␈↓αequal[A;A]␈↓, and:

␈↓ ↓H␈↓α␈↓ αX  equal[A;A] =␈↓ ∧_[atom[A] → [atom[A] → eq[A;A]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧_ atom[A] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧_ equal [car[A];car[A]] → equal[cdr[A];cdr[A]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧_ ␈↓
t␈↓α → ␈↓
f␈↓α]

␈↓ ↓H␈↓This␈α≠conditional␈α≤expression␈α≠will␈α≤finally␈α≠evaluate␈α≤to␈α≠␈↓
t␈↓.␈α≠So␈α≤p␈↓β3␈↓␈α≠in␈α≤the␈α≠original␈α≤call␈α≠of
␈↓ ↓H␈↓␈↓αequal[(A . B);(A . C)]␈↓␈α≥is␈α≥true,␈α≥and␈α≥we␈α≥are␈α≥faced␈α≥with␈α≥the␈α≥evaluation␈α≥of␈α≥e␈↓β3␈↓␈α≥which␈α≤is
␈↓ ↓H␈↓␈↓αequal[cdr[(A . B);cdr(A . C)]]␈↓.␈α
After␈αevaluation␈α
of␈α
the␈αarguments␈α
and␈αevaluation␈α
of␈α
the␈αconditional
␈↓ ↓H␈↓expression␈αdefining␈α␈↓αequal␈↓␈αwe␈αwill␈αfinally␈αreturn␈αvalue␈α␈↓
f␈↓.␈αThat␈αis,␈α␈↓α(A␈α.␈αB)␈↓␈αand␈α␈↓α(A␈α.␈αC)␈↓␈αare␈α␈↓↓not␈↓␈αequal.
␈↓ ↓H␈↓Clearly␈α
evaluation␈α∞of␈α
LISP␈α
expressions␈α∞in␈α
this␈α
great␈α∞detail␈α
is␈α
not␈α∞a␈α
process␈α
which␈α∞we␈α
wish␈α∞to␈α
do
␈↓ ↓H␈↓␈↓↓28  Symbolic expressions␈↓ 42.5␈↓


␈↓ ↓H␈↓very␈α
often.␈α
It␈α
might␈α
perhaps␈α
be␈α
clear␈α
that␈α
such␈α
a␈α
process␈α
is␈α
a␈α
likely␈α
candidate␈α
for␈α
execution␈α
by␈αa
␈↓ ↓H␈↓machine.

␈↓ ↓H␈↓Notice␈α∪that␈α∪throughout␈α∪this␈α∪example␈α∪expressions␈α∪like␈α∪␈↓αatom[(A . B)]␈↓␈α∪or␈α∪␈↓αeq[(A . B);(A . C)]␈↓␈α∪were
␈↓ ↓H␈↓appearing␈α∩but␈α∪were␈α∩never␈α∪evaluated␈α∩because␈α∪of␈α∩the␈α∩way␈α∪in␈α∩which␈α∪we␈α∩defined␈α∪evaluation␈α∩of
␈↓ ↓H␈↓conditionals.

␈↓ ↓H␈↓Finally, to include conditional expressions in our syntax of LISP expressions, we should add:

␈↓ ↓H␈↓<form>␈↓ αh::= <conditional expression>
␈↓ ↓H␈↓<conditional expression>␈↓ ∧X::= [<form> → <form>; ...; <form> → <form>]         (see page 20)


␈↓ ↓H␈↓These␈α⊂syntax␈α∂equations␈α⊂fail␈α∂to␈α⊂capture␈α⊂all␈α∂of␈α⊂our␈α∂intended␈α⊂meaning.␈α∂For␈α⊂example,␈α⊂the␈α∂<form>s
␈↓ ↓H␈↓appearing␈αin␈α
the␈αp␈↓βi␈↓-position␈αare␈α
restricted␈αto␈αbe␈α
forms␈αtaking␈α
values␈αin␈α␈↓	Tr␈↓,␈α
the␈αtruth␈αdomain.␈α
That
␈↓ ↓H␈↓restriction␈αis␈α
not␈αexpressed␈α
in␈αthe␈α
equations,␈αand␈α
indeed,␈αis␈α
difficult␈αto␈α
express␈αin␈α
a␈αnatural␈α
manner.
␈↓ ↓H␈↓See [Hop xx] for a discussion of expressibility and grammars.  



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓I Evaluate the following:

␈↓ ↓H␈↓␈↓ βR␈↓↓1.␈↓α eq[X;Y]   ␈↓↓2.␈↓α cons[X;Y]   ␈↓↓3.␈↓α car[(X . Y)]   ␈↓↓4.␈↓α car[cons[X;Y]]

␈↓ ↓H␈↓α␈↓↓5.␈↓α cadr[(X .(Y . NIL))]␈↓ ε8␈↓↓6.␈↓α cdar[(X .(Y . NIL))]

␈↓ ↓H␈↓α␈↓↓7.␈↓α eq[cdr[(A . B)];cdr[(C . B)]]␈↓ ε8␈↓↓8.␈↓α atom[cons[(A . B);(C . D)]]

␈↓ ↓H␈↓α␈↓↓9.␈↓α cons[atom[A];atom[(A . B)]]␈↓ ε8␈↓↓10.␈↓α eq[atom[ATOM];atom[EQ]]

␈↓ ↓H␈↓α␈↓ βk␈↓↓11.␈↓α [␈↓
t␈↓α → A; ␈↓
t␈↓α → B]   ␈↓↓12.␈↓α [␈↓
f␈↓α → A; ␈↓
t␈↓α → B]   ␈↓↓13.␈↓α [eq[A;B] → 4]

␈↓ ↓H␈↓α␈↓↓14.␈↓α [atom[X] → atom[X]; ␈↓
t␈↓α → FOO]␈↓ ε8␈↓↓15.␈↓α [eq[EQ; X] → A; eq[A; B] → B; ␈↓
t␈↓α → C]
␈↓ ↓H␈↓α␈↓ βT␈↓↓16.␈↓α cons[[eq[A; B] → 1; ␈↓
t␈↓α → FOO]; cons[A; cadr[(A .(B .C))]]]

␈↓ ↓H␈↓α␈↓↓17.␈↓α equal[(A . B);(A . B)]␈↓ ε8␈↓↓18.␈↓α eq[(A . B);(A . B)]
␈↓ ↓H␈↓␈↓↓2.5␈↓ εsPredicates and Conditional Expressions     29␈↓



␈↓ ↓H␈↓II  Use the following definition:

␈↓ ↓H␈↓α␈↓ αX  twist[s] <=␈↓ ∧([atom[s] → s;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( ␈↓
t␈↓α → cons[twist[cdr[s]];twist[car[s]]]]

␈↓ ↓H␈↓α␈↓↓1␈↓. Is the function partial or is it total?

␈↓ ↓H␈↓Now evaluate:
␈↓ ↓H␈↓␈↓↓2.␈↓α twist[A]   ␈↓↓3.␈↓α twist[(A . B)]   ␈↓↓4.␈↓α twist[((A . B). C)]

␈↓ ↓H␈↓III  Now try:
␈↓ ↓H␈↓α␈↓ αXfindem[x;y] <=␈↓ ∧([atom[x] → [eq[x;y] → T; ␈↓
t␈↓α → NIL];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( ␈↓
t␈↓α → cons[findem[car[x];y];findem[cdr[x];y]]]


␈↓ ↓H␈↓α␈↓↓1␈↓.  Is this function total?

␈↓ ↓H␈↓and now evaluate:
␈↓ ↓H␈↓␈↓ α⎇␈↓↓2.␈↓α findem[(A . B);A]   ␈↓↓3.␈↓α findem[(B .(A . C));A]  ␈↓↓4.␈↓α findem[(B .(A . C));C]
␈↓ ↓H␈↓α␈↓ ¬-␈↓↓5.␈↓α findem[(A . B);(A . B)]




␈↓ ↓H␈↓␈↓ ∧K␈↓↓2.6  Sequences: abstract data structures␈↓


␈↓ ↓H␈↓In␈αseveral␈αareas␈αof␈αmathematics␈αit␈αis␈αconvenient␈αto␈αdeal␈αwith␈αsequences␈αof␈αinformation,␈αthat␈αis,␈αthe
␈↓ ↓H␈↓problem␈α∃domain␈α∃more␈α∀naturally␈α∃described␈α∃as␈α∃collections␈α∀of␈α∃numbers␈α∃rather␈α∃than␈α∀individual
␈↓ ↓H␈↓numbers.␈α
 This␈α
may␈α
either␈α∞simplify␈α
understanding␈α
of␈α
the␈α∞problem␈α
or␈α
simplify␈α
the␈α∞formulation␈α
of
␈↓ ↓H␈↓the␈α∩functions␈α∩defined␈α∩on␈α∩the␈α∩domain.␈α∩ Thus␈α∩several␈α∩common␈α∩programming␈α∩languages␈α⊃include
␈↓ ↓H␈↓arrays␈α∞as␈α∂representations␈α∞of␈α∞these␈α∂mathematical␈α∞ideas.␈α∂ First␈α∞we␈α∞should␈α∂notice␈α∞that␈α∂sequences␈α∞are
␈↓ ↓H␈↓data␈αstructures.␈αWe␈αwill␈α
have␈αto␈αdescribe␈αconstructors,␈αselectors,␈α
and␈αrecognizers␈αfor␈αthem.␈α
Also␈αwe
␈↓ ↓H␈↓will␈αexplore␈αapplications␈αof␈αsequences␈αas␈αdata␈αstructures␈αsuitable␈αfor␈αrepresenting␈αmany␈αnon-trivial
␈↓ ↓H␈↓problems in computer science.

␈↓ ↓H␈↓After␈α
a␈α
certain␈α
familarity␈α
is␈α
gained␈α
in␈α
the␈α
application␈α
of␈α
algorithms␈α
which␈α∞manipulate␈α
sequences,
␈↓ ↓H␈↓we␈α
will␈α
discuss␈αthe␈α
problems␈α
of␈α
representation␈αand␈α
implementation␈α
of␈αthis␈α
data␈α
structure.␈α
We␈αwill
␈↓ ↓H␈↓first␈α∞give␈α
an␈α∞implementation␈α
of␈α∞sequences␈α
in␈α∞terms␈α
of␈α∞S-expressions.␈α
That␈α∞is,␈α
we␈α∞will␈α∞describe␈α
an
␈↓ ↓H␈↓␈↓λr␈↓-mapping␈αgiving␈αa␈αrepresentation␈αof␈αsequences␈αand␈αtheir␈αprimitive␈αoperations␈αin␈αterms␈αof␈αLISP's
␈↓ ↓H␈↓S-exprs␈α⊃and␈α⊃primitive␈α⊃functions.␈α⊃Later␈α∩in␈α⊃Section ␈α⊃we␈α⊃will␈α⊃discuss␈α⊃low-level␈α∩implementation␈α⊃of
␈↓ ↓H␈↓this data structure in terms of conventional machines.

␈↓ ↓H␈↓But␈α⊂first␈α⊂we␈α⊂will␈α∂study␈α⊂sequences␈α⊂as␈α⊂abstract␈α⊂data␈α∂structures:␈α⊂what␈α⊂are␈α⊂their␈α⊂essential␈α∂structural
␈↓ ↓H␈↓␈↓↓30  Symbolic expressions␈↓ 52.6␈↓


␈↓ ↓H␈↓characteristics?␈α What␈αproperties␈αshould␈αbe␈αpresent␈αin␈αa␈αprogramming␈αlanguage␈αto␈αallow␈α
a␈αnatural
␈↓ ↓H␈↓and␈α∃flexible␈α∃representation?␈α∃ This␈α∃discussion␈α∃will␈α∃shed␈α∃light␈α∃on␈α∃the␈α∃important␈α∃problems␈α∃of
␈↓ ↓H␈↓representation and abstraction.

␈↓ ↓H␈↓A␈αsequence␈α
is␈αan␈αordered␈α
set␈αof␈αelements␈↓π 19␈↓.␈α
 For␈αexample,␈α␈↓↓(x␈↓β1␈↓↓,␈α
x␈↓β2␈↓↓,␈αx␈↓β3␈↓↓)␈↓,␈αis␈α
standard␈αnotation␈α
for␈αa
␈↓ ↓H␈↓sequence␈α∂of␈α∂the␈α∂three␈α∂elements␈α∂␈↓↓x␈↓β1␈↓↓,␈α∂x␈↓β2␈↓,␈α∂and␈α⊂␈↓↓x␈↓β3␈↓.␈α∂ The␈α∂length␈α∂of␈α∂a␈α∂sequence␈α∂is␈α∂defined␈α∂to␈α⊂be␈α∂the
␈↓ ↓H␈↓number␈α∩of␈α∩elements␈α∩in␈α∩that␈α∩sequence.␈α∪ We␈α∩will␈α∩allow␈α∩sequences␈α∩to␈α∩have␈α∩sub-sequences␈α∪to␈α∩an
␈↓ ↓H␈↓arbitrary␈α⊃finite␈α⊃depth.␈α⊃ That␈α⊃is,␈α⊃the␈α⊃elements␈α⊃of␈α⊃a␈α⊃sequence␈α⊃will␈α⊃either␈α⊃be␈α⊃individuals␈α⊃or␈α⊂may
␈↓ ↓H␈↓themselves␈α
be␈α
sequences.␈α
For␈α
example,␈α
a␈α
sequence␈α
of␈α
length␈α
␈↓αn␈↓,␈α
each␈α
of␈α
whose␈α
elements␈α
are␈α
sequences
␈↓ ↓H␈↓of length ␈↓αm␈↓, is a matrix.  Here are BNF equations for sequences and their elements:

␈↓ ↓H␈↓<seq>␈↓ β(::= ␈↓↓(␈↓ <seq elem>, ...,<seq elem> ␈↓↓)␈↓ ␈↓π 20␈↓
␈↓ ↓H␈↓<seq elem>␈↓ β(::= <indiv> | <seq>
␈↓ ↓H␈↓<indiv>␈↓ β(:: = <literal indiv>|<numeral>| -<numeral>
␈↓ ↓H␈↓<literal indiv>␈↓ β(:: = <indiv letter>|<literal indiv><indiv letter>|<literal indiv><digit>
␈↓ ↓H␈↓<numeral>␈↓ β(:: = <digit>|<numeral><digit>
␈↓ ↓H␈↓<indiv letter>␈↓ β(:: =␈↓↓ A |B |C |D |E |F |G |H |I |J |K |L |M |N |O |P |Q |R |S |T |U |V |W |X |Y |Z␈↓
␈↓ ↓H␈↓<digit>␈↓ β(:: = ␈↓↓0 |1 |2 |3 |4 |5 |6 |7 |8 |9␈↓

␈↓ ↓H␈↓Notice␈αthat␈αthe␈α
structure␈αof␈α<indiv>␈α
is␈αthe␈αsame␈α
as␈αthat␈αfor␈α
LISP's␈α<atom>;␈αthe␈α
only␈αdifference␈αis␈α
in
␈↓ ↓H␈↓the␈α⊃fonts␈α⊃used␈α⊃for␈α⊃letters␈α⊃and␈α⊃digits.␈α⊂We␈α⊃have␈α⊃made␈α⊃the␈α⊃distinction␈α⊃between␈α⊃LISP␈α⊃atoms␈α⊂and
␈↓ ↓H␈↓sequence␈α
individuals␈α
intentionally.␈α
 Thus␈α
␈↓↓(A, (B, C), D, (E, B))␈↓␈α∞is␈α
a␈α
sequence␈α
of␈α
length␈α∞four,␈α
whose
␈↓ ↓H␈↓second and fourth elements are also sequences.  We will use "␈↓↓()␈↓" as notation for the empty sequence.

␈↓ ↓H␈↓We␈α∞want␈α∞to␈α∂write␈α∞LISP-like␈α∞functions␈α∞operating␈α∂over␈α∞sequences,␈α∞so␈α∞we␈α∂will␈α∞at␈α∞least␈α∞need␈α∂to␈α∞give
␈↓ ↓H␈↓constructors,␈α
selectors␈α∞and␈α
predicates␈α∞for␈α
sequences␈↓π 21␈↓.␈α∞As␈α
in␈α∞the␈α
case␈α∞of␈α
Symbolic␈α∞expressions,␈α
we
␈↓ ↓H␈↓will include the undefined element, and the full domain of sequences will be named
␈↓ ↓H␈↓␈↓ ¬M␈↓	Seq␈↓ = ␈↓<seq>␈↓∪{␈↓λB␈↓}.

␈↓ ↓H␈↓As on page 22, we extend the primitive LISP operations to include this new domain, by defining:
␈↓ ↓H␈↓␈↓ ¬b␈↓	S␈↓β2␈↓ = ␈↓	S␈↓β1␈↓∪␈↓<seq>␈↓,

␈↓ ↓H␈↓and extend each operation appropriately over ␈↓	S␈↓β2␈↓. Thus, for example:
␈↓ ↓H␈↓α␈↓ ¬}atom[␈↓↓A␈↓α] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ε	car[␈↓↓A␈↓α] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ¬ocar[␈↓↓(A, B)␈↓α] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ¬pcons[␈↓↓A␈↓; ␈↓↓B␈↓α] = ␈↓λB␈↓α
␈↓ ↓H␈↓α␈↓ ¬missexpr[␈↓↓(A)␈↓α] = ␈↓
f␈↓α
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 19␈↓␈α∩For␈α∩an␈α∩alternative␈α∩description␈α∩of␈α∩sequences␈α∩and␈α∩a␈α∩discussion␈α∩of␈α∩a␈α∩different␈α∩view␈α∩of␈α⊃data
␈↓ ↓H␈↓structures see page 42.

␈↓ ↓H␈↓␈↓π 20␈↓ For the meaning of these ellipses see page 20.

␈↓ ↓H␈↓␈↓π 21␈↓␈α⊃Ultimately␈α∩we␈α⊃will␈α⊃want␈α∩to␈α⊃give␈α⊃a␈α∩representation␈α⊃for␈α⊃sequences␈α∩as␈α⊃S-expressions,␈α∩and␈α⊃give
␈↓ ↓H␈↓representations for the sequence operations as operations on S-expressions.
␈↓ ↓H␈↓␈↓↓2.6␈↓ π6Sequences: abstract data structures     31␈↓


␈↓ ↓H␈↓We␈α∂need␈α∂to␈α∂define␈α∞some␈α∂data␈α∂structure␈α∂operations␈α∂specific␈α∞to␈α∂sequences.␈α∂ What␈α∂are␈α∂the␈α∞essential
␈↓ ↓H␈↓characteristics␈α
of␈αa␈α
sequence?␈αFirst,␈α
a␈α
sequence␈αeither␈α
is␈αempty␈α
or␈α
has␈αelements.␈α
Thus␈αwe␈α
will␈αwant␈α
a
␈↓ ↓H␈↓predicate␈α∂to␈α∞test␈α∂for␈α∂emptyness.␈α∞ Next,␈α∂if␈α∂the␈α∞sequence␈α∂is␈α∞non-empty,␈α∂we␈α∂should␈α∞be␈α∂able␈α∂to␈α∞select
␈↓ ↓H␈↓elements. Finally, given some elements, we should be able to build a new sequence from them.

␈↓ ↓H␈↓The␈α⊂basic␈α∂predicate,␈α⊂which␈α∂tests␈α⊂for␈α∂emptyness,␈α⊂is␈α∂called␈α⊂␈↓αnull␈↓.␈α∂ Predicates␈α⊂on␈α∂sequences␈α⊂are␈α∂like
␈↓ ↓H␈↓predicates on S-expressions, mapping sequences to truth values in ␈↓	Tr␈↓␈↓π 22␈↓.

␈↓ ↓H␈↓␈↓ αX␈↓ βx␈↓
t␈↓ if ␈↓αx␈↓ is the empty sequence, ␈↓↓()␈↓.
␈↓ ↓H␈↓␈↓ αX␈↓αnull[x]␈↓ is␈↓ βx␈↓
f␈↓ if ␈↓αx␈↓ is a non-empty sequence.
␈↓ ↓H␈↓␈↓ αX␈↓ βx␈↓λB␈↓ otherwise.

␈↓ ↓H␈↓␈↓ εε␈↓αnull[␈↓↓( )␈↓α] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ¬nnull[␈↓↓(A, B)␈↓α] = ␈↓
f␈↓α
␈↓ ↓H␈↓α␈↓ επnull[␈↓
f␈↓α] = ␈↓λB␈↓


␈↓ ↓H␈↓Thus␈α␈↓αnull␈↓␈αgives␈αuseable␈α
values␈αonly␈αfor␈αsequences.␈α Since␈α
we␈αintend␈αto␈αoperate␈αon␈α
domains␈αwhich
␈↓ ↓H␈↓contain␈αdata␈α
structures␈αother␈αthan␈α
sequences,␈αwe␈αwill␈α
need␈αa␈αrecognizer␈α
to␈αbe␈αsure␈α
that␈α␈↓αnull␈↓␈α
is␈αnot
␈↓ ↓H␈↓applied to arguments which are ␈↓↓not␈↓ sequences.  We will name this recognizer ␈↓αisseq␈↓.

␈↓ ↓H␈↓␈↓ ¬Y␈↓αisseq[␈↓↓(A, B, C)␈↓α] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ ε¬isseq[␈↓↓A␈↓α] = ␈↓
f␈↓
␈↓ ↓H␈↓␈↓ ¬7␈↓αisseq[A] = ␈↓
f␈↓  ␈↓αisseq[␈↓
t␈↓α] = ␈↓
f␈↓
␈↓ ↓H␈↓␈↓ ελ␈↓αisseq[␈↓↓()␈↓α] = ␈↓
t␈↓
␈↓ ↓H␈↓␈↓ ¬}␈↓αisseq[␈↓λB␈↓α] = ␈↓λB␈↓


␈↓ ↓H␈↓Thus ␈↓αisseq␈↓ is a total predicate over all domains, whereas ␈↓αnull␈↓ is only partial, total over ␈↓<seq>␈↓.

␈↓ ↓H␈↓While␈α∂on␈α∂the␈α∂subject␈α∂of␈α⊂predicates,␈α∂there␈α∂are␈α∂a␈α∂couple␈α∂more␈α⊂we␈α∂shall␈α∂need.␈α∂ The␈α∂first␈α∂one␈α⊂is␈α∂a
␈↓ ↓H␈↓recognizer,␈α
␈↓αisindiv␈↓,␈αwhich␈α
will␈αgive␈α
value␈α␈↓
t␈↓␈α
if␈αits␈α
argument␈α
is␈αan␈α
individual,␈αgive␈α
␈↓
f␈↓␈αif␈α
its␈αargument␈α
is
␈↓ ↓H␈↓a sequence, and will give ␈↓λB␈↓ otherwise.

␈↓ ↓H␈↓The␈αsecond␈αpredicate␈αis␈αthe␈αextension␈αof␈αthe␈αequality␈αrelation␈αto␈αthe␈αclass␈αof␈α
sequence␈αindividuals.
␈↓ ↓H␈↓We␈α
shall␈α∞use␈α
the␈α∞same␈α
name,␈α∞␈↓αeq␈↓,␈α
as␈α
we␈α∞did␈α
for␈α∞the␈α
S-expression␈α∞predicate.␈α
 Indeed,␈α∞whenever␈α
we
␈↓ ↓H␈↓define␈α
a␈α
new␈αabstract␈α
data␈α
type␈αwe␈α
will␈α
assume␈α
that␈αan␈α
appropriate␈α
version␈αof␈α
␈↓αeq␈↓␈α
is␈α
available␈αfor
␈↓ ↓H␈↓the␈αelements␈α
of␈αthe␈αbase␈α
domain.␈αOne␈αof␈α
our␈αfirst␈αtasks␈α
will␈αbe␈αto␈α
extend␈αthat␈αequality␈α
relation␈αto
␈↓ ↓H␈↓the␈αwhole␈αdomain.␈αWe␈αwill␈αdo␈αso␈αfor␈αsequences␈αlater␈αin␈αthis␈αsection.␈αEquality␈αis␈αa␈αbasic␈αrelation␈αin

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 22␈↓␈α⊂The␈α∂reason␈α⊂for␈α∂restructuring␈α⊂LISP␈α∂predicates␈α⊂should␈α∂now␈α⊂be␈α∂apparent␈α⊂to␈α∂previous␈α⊂users␈α∂of
␈↓ ↓H␈↓LISP:␈αif␈αwe␈αmapped␈αthe␈αtruth␈αvalues␈αto␈αthe␈αatoms␈α␈↓αT␈↓␈αand␈α␈↓αNIL␈↓␈αas␈αis␈αtypically␈αdone,␈αthen␈αwe'd␈αhave
␈↓ ↓H␈↓to␈α
map␈αtruth␈α
values␈α
of␈αsequence-predicates␈α
to␈α
representation␈αas␈α
sequences.␈α
Indeed␈αwe␈α
would␈αhave␈α
to
␈↓ ↓H␈↓perpetuate␈α∞the␈α∞fraud␈α∞for␈α∞every␈α∞new␈α∂abstract␈α∞data␈α∞structure␈α∞domain␈α∞that␈α∞we␈α∞wanted␈α∂to␈α∞introduce.
␈↓ ↓H␈↓Thus we did it right the first time.
␈↓ ↓H␈↓␈↓↓32  Symbolic expressions␈↓ 52.6␈↓


␈↓ ↓H␈↓mathematics␈α∂so␈α∂it␈α∂is␈α⊂not␈α∂surprising␈α∂to␈α∂see␈α∂it␈α⊂play␈α∂an␈α∂important␈α∂role␈α∂here.␈α⊂ ␈↓αeq␈↓␈α∂is␈α∂one␈α∂of␈α⊂the␈α∂few
␈↓ ↓H␈↓relations␈α⊃which␈α⊃we␈α⊃shall␈α⊃define␈α⊃across␈α⊃all␈α⊃domains.␈α⊃Functions␈α⊃or␈α⊃predicates␈α⊃like␈α⊃␈↓αeq␈↓,␈α∩which␈α⊃are
␈↓ ↓H␈↓applicable on several domains, are called ␈↓↓polymorphic functions␈↓.

␈↓ ↓H␈↓Next, the selectors for a (non-empty) sequence include: ␈↓αfirst␈↓, ␈↓αsecond␈↓, ... ,etc, where:

␈↓ ↓H␈↓␈↓ ¬U␈↓αfirst[␈↓↓(A, B, C)␈↓α] = ␈↓↓A␈↓α
␈↓ ↓H␈↓α␈↓ ¬Jsecond[␈↓↓(A, B, C)␈↓α] = ␈↓↓B␈↓α
␈↓ ↓H␈↓α␈↓ ¬cthird[␈↓↓(A, B)␈↓α] = ␈↓λB␈↓

␈↓ ↓H␈↓It is also convenient to define an "all-but-first" selector, called ␈↓αrest␈↓.

␈↓ ↓H␈↓␈↓ ¬@␈↓αrest[␈↓↓(A, B, C)␈↓α] = ␈↓↓(B, C)␈↓α
␈↓ ↓H␈↓α␈↓ ¬drest[␈↓↓(B, C)␈↓α] = ␈↓↓(C)␈↓α
␈↓ ↓H␈↓α␈↓ εrest[␈↓↓(C)␈↓α] = ␈↓↓()␈↓α
␈↓ ↓H␈↓α␈↓ ε¬rest[␈↓↓C␈↓α] = ␈↓λB␈↓
␈↓ ↓H␈↓␈↓ εα␈↓αrest[␈↓↓( )␈↓α] = ␈↓λB␈↓

␈↓ ↓H␈↓In␈αconjunction␈α
with␈α␈↓αrest␈↓,␈α
we␈αshall␈α
utilize␈αa␈α
constructor,␈α␈↓αconcat␈↓,␈α
which␈αis␈α
to␈αadd␈α
a␈αsingle␈α
element␈αto
␈↓ ↓H␈↓the front of a sequence.
␈↓ ↓H␈↓␈↓ ¬&␈↓αconcat[␈↓↓A␈↓α;␈↓↓(B,C)␈↓α] = ␈↓↓(A, B, C)␈↓α
␈↓ ↓H␈↓α␈↓ ¬aconcat[␈↓↓A␈↓α;␈↓↓()␈↓α] = ␈↓↓(A)␈↓α
␈↓ ↓H␈↓α␈↓ ¬∃concat[␈↓↓(A)␈↓α;␈↓↓(B,C)␈↓α] = ␈↓↓((A), B, C)␈↓α
␈↓ ↓H␈↓α␈↓ ¬Rconcat[␈↓↓(B,C)␈↓α;␈↓↓A␈↓α] = ␈↓λB␈↓
␈↓ ↓H␈↓␈↓ ¬c␈↓αconcat[␈↓↓A␈↓α; ␈↓↓B␈↓α] = ␈↓λB␈↓


␈↓ ↓H␈↓The␈αfinal␈αconstructor␈αis␈αcalled␈α␈↓αseq␈↓;␈αit␈αtakes␈αan␈αarbitrary␈αnumber␈αof␈αsequence␈αelements␈αas␈αarguments
␈↓ ↓H␈↓and␈α∩returns␈α∩a␈α∩sequence␈α⊃consisting␈α∩of␈α∩those␈α∩elements␈α∩(in␈α⊃the␈α∩obvious␈α∩order).␈α∩ Let␈α∩␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓␈α⊃be
␈↓ ↓H␈↓elements of <seq elem>, then:

␈↓ ↓H␈↓α␈↓ ¬
seq[␈↓λα␈↓β1␈↓α; ␈↓λα␈↓β2␈↓α; ...; ␈↓λα␈↓βn␈↓α] = ␈↓↓(␈↓λα␈↓β1␈↓↓, ..., ␈↓λα␈↓βn␈↓↓)␈↓

␈↓ ↓H␈↓One␈αquestion␈α
may␈αhave␈α
come␈αto␈α
mind:␈αhow␈α
do␈αwe␈α
know␈αwhen␈α
we␈αhave␈α
a␈αsufficient␈α
set␈αof␈α
functions
␈↓ ↓H␈↓for␈αthe␈αmanipulation␈αof␈αan␈αabstract␈αdata␈αstructure?␈α How␈αdo␈αwe␈αknow␈αwe␈αhaven't␈αleft␈αsome␈αcrucial
␈↓ ↓H␈↓functions␈αout?␈αIf␈αwe␈αhave␈αenough,␈αhow␈α
do␈αwe␈αknow␈αthat␈αwe␈αhaven't␈αincluded␈αtoo␈α
many?␈αActually,
␈↓ ↓H␈↓this␈αsecond␈αcase␈αisn't␈αdisastrous,␈αbut␈αwhen␈αimplementing␈αthe␈αfunctions␈αit␈αwould␈αbe␈αnice␈αto␈α
minimize
␈↓ ↓H␈↓the␈α
number␈α
of␈α
primitives␈α
we␈α
have␈α
to␈α
program.␈α These␈α
problems␈α
are␈α
worthy␈α
of␈α
study␈α
and␈α
are␈αthe
␈↓ ↓H␈↓concern␈α
of␈α
anyone␈α
interested␈α
in␈α
the␈α
design␈α
of␈α
programming␈α
languages.␈α
We␈α
will␈α
say␈α
a␈α
bit␈α
more␈α
about
␈↓ ↓H␈↓solutions to these questions beginning on page 38.

␈↓ ↓H␈↓Notice␈α⊂that␈α⊂we␈α⊃have␈α⊂been␈α⊂describing␈α⊃the␈α⊂sequence␈α⊂functions␈α⊃without␈α⊂regard␈α⊂to␈α⊃any␈α⊂underlying
␈↓ ↓H␈↓representation.␈α
 We␈αhave␈α
said␈αnothing␈α
about␈αthese␈α
sequence␈αoperations␈α
except␈αthat␈α
they␈αconstruct,
␈↓ ↓H␈↓test,␈αor␈αselect.␈α What␈αwe␈αare␈αdoing␈αis␈αconsidering␈αsequences␈αas␈αabstract␈αdata␈αstructures,␈αsuitable␈αfor
␈↓ ↓H␈↓manipulation␈α∞by␈α
LISP-like␈α∞programs.␈α∞How␈α
sequences␈α∞are␈α
represented␈α∞on␈α∞a␈α
machine␈α∞or␈α∞indeed␈α
as
␈↓ ↓H␈↓␈↓↓2.6␈↓ π6Sequences: abstract data structures     33␈↓


␈↓ ↓H␈↓S-expressions,␈α∂is␈α∂irrelevant.␈α∞Sequences␈α∂have␈α∂certain␈α∞inherent␈α∂structural␈α∂properties␈α∞and␈α∂it␈α∂is␈α∞those
␈↓ ↓H␈↓properties␈α
which␈αwe␈α
must␈α
understand␈α␈↓↓before␈↓␈α
we␈αbegin␈α
thinking␈α
about␈αrepresentation␈α
on␈αa␈α
machine.
␈↓ ↓H␈↓In␈αthe␈αnext␈αsection␈αwe␈αwill␈αshow␈αhow␈αto␈αrepresent␈αsequences␈αas␈αcertain␈αS-expressions␈αand␈αsequence
␈↓ ↓H␈↓operations as LISP operations on that representation.

␈↓ ↓H␈↓First␈α∪let's␈α∩develop␈α∪some␈α∪expertise␈α∩in␈α∪manipulating␈α∩sequences.␈α∪ The␈α∪first␈α∩example␈α∪will␈α∪be␈α∩our
␈↓ ↓H␈↓promised␈α⊃extension␈α⊃of␈α⊃the␈α⊃equality␈α⊃relation␈α⊃to␈α⊃sequences.␈α⊃We␈α⊃perpetuate␈α⊃the␈α⊃name␈α⊃␈↓αequal␈↓␈α⊃from
␈↓ ↓H␈↓S-exprs,␈α∩and␈α∪the␈α∩basic␈α∪structure␈α∩of␈α∩the␈α∪definition␈α∩will␈α∪parallel␈α∩that␈α∩of␈α∪its␈α∩namesake;␈α∪but␈α∩the
␈↓ ↓H␈↓components␈α∂of␈α⊂the␈α∂definition␈α∂will␈α⊂involve␈α∂sequence␈α∂operations␈α⊂rather␈α∂than␈α∂S-expr␈α⊂operations.␈α∂It
␈↓ ↓H␈↓might be of value to compare the two predicates.  The S-expr version is to be found on page 27.

␈↓ ↓H␈↓α     equal[x;y] <=␈↓ βh[null[x] → [null[y] → ␈↓
t␈↓α; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βh null[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βh equal␈↓λ'␈↓α [first[x];first[y]] → equal[rest[x];rest[y]];
␈↓ ↓H␈↓α␈↓ βh ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓ ↓H␈↓α     equal␈↓λ'␈↓α[x;y] <=␈↓ βh[isindiv[x] → [isindiv[y] → eq[x;y]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βh isindiv[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βh ␈↓
t␈↓α → equal[x;y]]

␈↓ ↓H␈↓Next,␈αwe␈αwill␈αwrite␈αa␈αpredicate␈α␈↓αmember␈↓␈αof␈αtwo␈αarguments␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓.␈α ␈↓αx␈↓␈αis␈αto␈αbe␈αan␈αindividual;␈α␈↓αy␈↓␈αis␈αto
␈↓ ↓H␈↓be␈αa␈αsequence;␈α␈↓αmember␈↓␈αis␈αto␈αreturn␈α␈↓
t␈↓␈αjust␈αin␈αthe␈αcase␈αthat␈α␈↓αx␈↓␈αis␈αan␈αelement␈αof␈αthe␈αsequence␈α␈↓αy␈↓.␈α What
␈↓ ↓H␈↓does␈α
this␈αspecification␈α
tell␈αus?␈α
 The␈αpredicate␈α
is␈α
partial.␈αThe␈α
recursion␈αshould␈α
be␈αon␈α
the␈αstructure␈α
of
␈↓ ↓H␈↓␈↓αy␈↓;␈αand␈αtermination␈α(with␈αvalue␈α␈↓
f␈↓)␈αshould␈αoccur␈αif␈α␈↓αy␈↓␈αis␈αthe␈αempty␈αsequence.␈αIf␈α␈↓αy␈↓␈αis␈αnot␈αempty␈αthen␈αit
␈↓ ↓H␈↓has␈αa␈αfirst␈αelement␈α(call␈αit␈α␈↓αz␈↓);␈αcompare␈α␈↓αz␈↓␈αwith␈α␈↓αx␈↓.␈α If␈αthese␈αelements␈αare␈αidentical␈αthen␈α␈↓αmember␈↓␈αshould
␈↓ ↓H␈↓return ␈↓
t␈↓; otherwise see if ␈↓αx␈↓ occurs in the remainder of the sequence ␈↓αy␈↓.

␈↓ ↓H␈↓Notes:

␈↓ ↓H␈↓␈↓↓1.␈↓␈αWe␈αcannot␈α
use␈α␈↓αeq␈↓␈αto␈α
check␈αequality␈αsince,␈α
though␈α␈↓αx␈↓␈αis␈α
an␈αindividual,␈αthere␈α
is␈αno␈αreason␈αto␈α
believe
␈↓ ↓H␈↓that the elements of ␈↓αy␈↓ need be.

␈↓ ↓H␈↓␈↓↓2.␈↓ Recall that we can get the first element of a sequence with ␈↓αfirst␈↓, and the rest of a list with ␈↓αrest␈↓.

␈↓ ↓H␈↓So here's ␈↓αmember␈↓:
␈↓ ↓H␈↓α␈↓ β8member[x;y] <=␈↓ ¬8[null[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ β8␈↓ ¬8 equal␈↓λ'␈↓α[first[y];x] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ β8␈↓ ¬8 ␈↓
t␈↓α → member[x;rest[y]]]


␈↓ ↓H␈↓Finally here is an arithmetic example to calculate the number of elements in a sequence.
␈↓ ↓H␈↓α␈↓ β{length[n] <= [null[n] → 0; ␈↓
t␈↓α → plus[1;length[rest[n]]]]
␈↓ ↓H␈↓␈↓↓34  Symbolic expressions␈↓ 52.7␈↓


␈↓ ↓H␈↓␈↓ ∧M␈↓↓2.7  Lists: representations of sequences␈↓


␈↓ ↓H␈↓It␈α
is␈αall␈α
well␈αand␈α
good␈αto␈α
be␈α
able␈αto␈α
write␈αLISP-like␈α
functions␈αdescribing␈α
operations␈α
on␈αsequences.
␈↓ ↓H␈↓The␈α∞algorithms␈α∞are␈α∞clean␈α∞and␈α
understandable.␈α∞ However,␈α∞if␈α∞we␈α∞wish␈α
to␈α∞run␈α∞these␈α∞programs␈α∞in␈α
a
␈↓ ↓H␈↓LISP␈α
environment,␈α
then␈αwe␈α
had␈α
better␈α
be␈αprepared␈α
to␈α
represent␈α
both␈αthe␈α
data␈α
structures␈α
and␈αthe
␈↓ ↓H␈↓algorithms␈αin␈αterms␈αunderstandable␈αto␈αLISP␈↓π 23␈↓.␈αThis␈αis␈αthe␈αproblem␈αof␈αrepresentation.␈αGranted,␈αwe
␈↓ ↓H␈↓could␈α→have␈α~overcome␈α→the␈α~problem␈α→by␈α~explicitly␈α→representing␈α~sequences␈α→directly␈α~as␈α→LISP
␈↓ ↓H␈↓S-expressions␈α∩and␈α⊃could␈α∩have␈α⊃written␈α∩functions␈α⊃in␈α∩LISP␈α⊃which␈α∩used␈α⊃␈↓αcar-cdr␈↓-chains␈α∩to␈α⊃directly
␈↓ ↓H␈↓manipulate␈α∃the␈α∃representations.␈α∃ This␈α∃misuse␈α∃of␈α∃representation␈α∃is␈α∃a␈α∃common␈α∃fault␈α⊗in␈α∃LISP
␈↓ ↓H␈↓programming␈α∞and␈α
its␈α∞practice␈α
is␈α∞to␈α
be␈α∞discouraged.␈α
 First,␈α∞the␈α
resulting␈α∞programs␈α
are␈α∞much␈α
more
␈↓ ↓H␈↓difficult␈αto␈αread␈αand␈αdebug␈αand␈αunderstand.␈α More␈αimportant,␈αthe␈αprograms␈αare␈αexplicitly␈αtied␈αto␈αa
␈↓ ↓H␈↓specific␈α
representation␈αof␈α
the␈αabstract␈α
data␈αstructure;␈α
if␈αat␈α
some␈αlater␈α
date␈αit␈α
is␈αdesired␈α
to␈αchange␈α
the
␈↓ ↓H␈↓representation,␈α∪then␈α∪many␈α∀programs␈α∪will␈α∪have␈α∀to␈α∪be␈α∪rewritten.␈α∀ In␈α∪Section 3.3␈α∪we␈α∀develop␈α∪a
␈↓ ↓H␈↓complex␈α⊃algorithm␈α⊃for␈α⊃differentiation␈α⊂on␈α⊃a␈α⊃class␈α⊃of␈α⊂polynomials,␈α⊃moving␈α⊃from␈α⊃an␈α⊃unclear␈α⊂and
␈↓ ↓H␈↓highly␈α~representation-dependent␈α~formulation,␈α~to␈α~a␈α~clear,␈α~concise,␈α~representation-independent
␈↓ ↓H␈↓algorithm.

␈↓ ↓H␈↓Obviously␈α⊃we␈α⊃will␈α∩always␈α⊃have␈α⊃to␈α∩supply␈α⊃a␈α⊃representational␈α∩bridge␈α⊃between␈α⊃the␈α∩abstract␈α⊃data
␈↓ ↓H␈↓structures␈α∩and␈α∩algorithms,␈α∪and␈α∩their␈α∩concrete␈α∪counterparts.␈α∩ One␈α∩aspect␈α∪of␈α∩this␈α∩study␈α∪of␈α∩data
␈↓ ↓H␈↓structures␈α
is␈α
to␈α
understand␈α
what␈α∞is␈α
required␈α
to␈α
build␈α
this␈α∞bridge␈α
and␈α
how␈α
best␈α
to␈α∞represent␈α
these
␈↓ ↓H␈↓requirements in a programming language.

␈↓ ↓H␈↓The␈α⊂first␈α⊂decision␈α⊃to␈α⊂be␈α⊂made␈α⊃is␈α⊂how␈α⊂to␈α⊃represent␈α⊂the␈α⊂abstract␈α⊃data␈α⊂structure;␈α⊂how␈α⊃should␈α⊂we
␈↓ ↓H␈↓represent␈α⊂sequences␈α⊂as␈α⊂S-expressions?␈α⊂Indeed␈α⊂how␈α⊂should␈α⊂we␈α⊂choose␈α⊂representations␈α⊃in␈α⊂general?
␈↓ ↓H␈↓Usually␈α∪there␈α∀is␈α∪not␈α∪just␈α∀one␈α∪"best"␈α∪representation.␈α∀Some␈α∪obvious␈α∪considerations␈α∀involve␈α∪the
␈↓ ↓H␈↓difficulty␈α_of␈α↔implementing␈α_the␈α↔primitive␈α_operations␈α↔(constructors,␈α_selectors,␈α_recognizers,␈α↔and
␈↓ ↓H␈↓predicates)␈α⊂on␈α⊂the␈α∂abstract␈α⊂data␈α⊂structure.␈α⊂Also␈α∂we␈α⊂must␈α⊂keep␈α⊂in␈α∂mind␈α⊂the␈α⊂kinds␈α⊂of␈α∂algorithms
␈↓ ↓H␈↓which␈αwe␈αwish␈αto␈αwrite;␈αcomputation␈αtakes␈αtime,␈αand␈αsince␈αthis␈αis␈αcomputer␈αscience␈αwe␈αshould␈αgive
␈↓ ↓H␈↓consideration to efficiency.











␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 23␈↓␈α⊂Indeed␈α⊂if␈α⊃we␈α⊂wish␈α⊂LISP␈α⊃to␈α⊂run␈α⊂on␈α⊂a␈α⊃conventional␈α⊂machine␈α⊂we␈α⊃had␈α⊂better␈α⊂be␈α⊃prepared␈α⊂to
␈↓ ↓H␈↓represent␈α∪LISP's␈α∩data␈α∪structures␈α∪and␈α∩algorithms␈α∪in␈α∪a␈α∩manner␈α∪understandable␈α∪to␈α∩conventional
␈↓ ↓H␈↓hardware. This task is the subject of later chapters in the book.
␈↓ ↓H␈↓%22.7␈↓ π5Lists: representations of sequences     35%*



␈↓ ↓H␈↓A reasonable choice for a representation of sequences as S-expressions is the following:
␈↓ ↓H␈↓␈↓ ¬=␈↓λr␈↓∞(␈↓<indiv>␈↓∞)␈↓ = ␈↓λ.␈↓β<atom>␈↓
␈↓ ↓H␈↓␈↓ ∧rand for ␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓ in ␈↓<seq elem>␈↓:
␈↓"␈↓ ↓H␈↓∂␈↓ αX      ␈↓λr␈↓∞(␈↓↓ (␈↓λα␈↓β1␈↓, ..., ␈↓λα␈↓βn␈↓↓) ␈↓∞)␈↓ = ␈↓∂␈↓ ¬h         /\
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h        /  \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h   ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓β1␈↓↓ ␈↓∞)␈↓∂ \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h             \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h              \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h              /\
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h             /  \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h        ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓β2␈↓↓ ␈↓∞)␈↓∂
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                 ...
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                   \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                   /\
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                  /  \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                 /    \
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h                /     ␈↓αNIL␈↓∂
␈↓"␈↓ ↓H␈↓∂␈↓ αX␈↓ ¬h           ␈↓λr␈↓∞(␈↓↓ ␈↓λα␈↓βn␈↓↓ ␈↓∞)

␈↓ ↓H␈↓That␈αis,␈αthe␈αright-hand␈αbranch␈αin␈αthis␈αLISP-tree␈αrepresentation␈αof␈αa␈αsequence␈αwill␈αalways␈α
point␈αto
␈↓ ↓H␈↓the␈αrest␈αof␈αthe␈αsequence␈αor␈αwill␈αbe␈αthe␈αatom␈α␈↓αNIL␈↓.␈α Notice␈αthat␈αthe␈αdescription␈αof␈αthe␈α␈↓λr␈↓-mapping␈αis
␈↓ ↓H␈↓recursive. Thus for example:
␈↓"␈↓ ↓H␈↓∂␈↓ β(    ␈↓λr␈↓∞(␈↓↓ ((A,B,C),(D)) ␈↓∞)␈↓ = ␈↓∂␈↓ ελ          /\
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ         /  \
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ        /    \
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ  ␈↓λr␈↓∞(␈↓↓ (A,B,C) ␈↓∞)␈↓∂ \
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ               \
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ                \
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ                /\
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ               /  \
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ              /   ␈↓αNIL␈↓∂
␈↓"␈↓ ↓H␈↓∂␈↓ β(␈↓ ελ         ␈↓λr␈↓∞(␈↓↓ (D) ␈↓∞)␈↓∂

␈↓ ↓H␈↓which␈α∂will␈α⊂finally␈α∂expand␈α∂to␈α⊂␈↓α((A␈α∂.(B␈α⊂.␈α∂(C␈α∂.␈α⊂NIL)))␈α∂.((D␈α⊂.␈α∂NIL)␈α∂.␈α⊂NIL))␈↓␈α∂since␈α⊂␈↓λr␈↓∞(␈↓↓ (A,B,C) ␈↓∞)␈↓␈α∂is
␈↓ ↓H␈↓␈↓α(A .(B .(C .NIL)))␈↓ and ␈↓λr␈↓∞(␈↓↓ (D) ␈↓∞)␈↓ is ␈↓α(D . NIL)␈↓.

␈↓ ↓H␈↓You␈α⊃should␈α⊃become␈α⊃fluent␈α⊂in␈α⊃translating␈α⊃between␈α⊃S-expr␈α⊂notation␈α⊃and␈α⊃sequence␈α⊃notation.␈α⊂ For
␈↓ ↓H␈↓convenience␈α↔sake␈α_we␈α↔will␈α_carry␈α↔over␈α_the␈α↔sequence␈α_notation␈α↔-- ␈↓↓(A, B, C)␈↓ --␈α_to␈α↔that␈α_for␈α↔the
␈↓ ↓H␈↓representation␈α∨in␈α∨LISP␈α≡-- ␈↓α(A, B, C)␈↓ --␈↓π 24␈↓␈α∨thinking␈α∨of␈α≡␈↓α(A, B, C)␈↓␈α∨as␈α∨an␈α∨abbreviation␈α≡for
␈↓ ↓H␈↓␈↓α(A .(B .(C . NIL)))␈↓.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓α␈↓π 24␈↓α ␈↓Be aware that ␈↓αA␈↓ is an atom and ␈↓↓A␈↓ is a sequence element; they are not the same data structure.
␈↓ ↓H␈↓␈↓↓36  Symbolic expressions␈↓ 52.7␈↓


␈↓ ↓H␈↓Next,␈α⊂what␈α⊂about␈α⊂a␈α∂representation␈α⊂for␈α⊂the␈α⊂empty␈α⊂sequence?␈α∂Looking␈α⊂at␈α⊂the␈α⊂representation␈α⊂of␈α∂a
␈↓ ↓H␈↓non-empty␈αsequence␈αit␈αappears␈α
natural␈αto␈αtake␈α␈↓αNIL␈↓␈α
as␈α␈↓λr␈↓∞(␈↓↓()␈↓∞)␈↓␈αsince␈αafter␈α
you␈αhave␈αremoved␈αall␈α
the
␈↓ ↓H␈↓elements from the sequence ␈↓αNIL␈↓ is all that is left in the representation.  To be consistent then:
␈↓ ↓H␈↓␈↓ ¬a␈↓λr␈↓∞(␈↓↓ ( ) ␈↓∞)␈↓ = ␈↓αNIL␈↓.

␈↓ ↓H␈↓This␈αgives␈αus␈αa␈αcomplete␈αspecification␈αof␈αthe␈α␈↓λr␈↓-mapping␈αfor␈αthe␈αdomain;␈αwe␈αhave␈αrepresented␈αthe
␈↓ ↓H␈↓abstract␈α⊂domain␈α⊂of␈α⊂sequences␈α⊃in␈α⊂a␈α⊂subset␈α⊂of␈α⊂the␈α⊃domain␈α⊂of␈α⊂Symbolic␈α⊂Expressions.␈α⊃The␈α⊂S-expr
␈↓ ↓H␈↓representation of a sequence is called a ␈↓↓list␈↓; and we will refer to the abbreviation,
␈↓ ↓H␈↓␈↓ ¬u␈↓α(␈↓λα␈↓β1␈↓α, ..., ␈↓λα␈↓βn␈↓α)␈↓ for

␈↓ ↓H␈↓␈↓ ∧9␈↓α(␈↓λα␈↓β1␈↓α .(␈↓λα␈↓β2␈↓α .  ... (␈↓λα␈↓βn␈↓α . NIL) ...)␈↓ as ␈↓↓list-notation␈↓.

␈↓ ↓H␈↓Thus␈αsequences␈αare␈α
the␈αabstract␈αdata␈α
structure;␈αlists␈αare␈αtheir␈α
representation.␈α Since␈αthe␈α
atom␈α␈↓αNIL␈↓
␈↓ ↓H␈↓takes on special significance in list-notation it is endowed with the special name ␈↓↓list terminator␈↓.

␈↓ ↓H␈↓And a notational point: in graphical interpretation of list-notation it is often convenient to write:
␈↓"␈↓ ↓H␈↓
␈↓ βx⊂αααπααααα⊃␈↓ πλ ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
␈↓ βx~   ~ NIL ~    ␈↓as␈↓
␈↓ πλ ~   ~≤'~
␈↓"␈↓ ↓H␈↓
␈↓ βx%ααα∀ααααα$␈↓ πλ %ααα∀αα$


␈↓ ↓H␈↓Thus, for example ␈↓α(A, (B, C), D)␈↓ is:

␈↓"␈↓ ↓H␈↓
                ⊂αααπααα⊃  ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                ~ A ~ #αβα→~ # ~ #αβα→~ D ~≤'~
␈↓"␈↓ ↓H␈↓
                %ααα∀ααα$  %αβα∀ααα$  %ααα∀αα$
␈↓"␈↓ ↓H␈↓
                             ~
␈↓"␈↓ ↓H␈↓
                             ~   ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                             %αα→~ B ~ #αβα→~ C ~≤'~
␈↓"␈↓ ↓H␈↓
                                 %ααα∀ααα$  %ααα∀αα$


␈↓ ↓H␈↓or,␈αin␈α"dotted-pair"␈αnotation:␈α␈↓α␈α(A␈α.((B␈α.(C␈α.␈αNIL)).(D␈α.␈αNIL)))␈↓.␈α Finally,␈αin␈αlist-notation␈αthe␈αcommas
␈↓ ↓H␈↓can be replaced by spaces␈↓π 25␈↓
␈↓ ↓H␈↓␈↓ ∧|e.g. ␈↓α(A, (B, C), D) = (A (B C) D).

␈↓ ↓H␈↓α␈↓but beware: the "dots" in dot-notation are ␈↓↓never␈↓ optional!

␈↓ ↓H␈↓␈↓ ∧␈that is   ␈↓α(A. (B. C)) ␈↓	≠␈↓α (A (B C)).

␈↓ ↓H␈↓Let's␈α∪take␈α∪stock␈α∪of␈α∪our␈α∪position:␈α∪We␈α∪have␈α∪an␈α∪intuitive␈α∪understanding␈α∪of␈α∪what␈α∪we␈α∀mean␈α∪by
␈↓ ↓H␈↓"sequence".␈α
We␈α
have␈α
described␈α
selectors,␈α
constructors,␈αand␈α
recognizers,␈α
albeit␈α
at␈α
an␈α
abstract␈αlevel,␈α
for
␈↓ ↓H␈↓manipulating␈α∃sequences.␈α⊗We␈α∃have␈α⊗represented␈α∃our␈α⊗notion␈α∃of␈α⊗sequences␈α∃as␈α⊗a␈α∃subset␈α⊗of␈α∃the

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 25␈↓␈αThis␈α
convention␈αis␈α
one␈αof␈αthe␈α
few␈αinstances␈α
of␈αa␈α"good"␈α
bug.␈αThe␈α
early␈αLISP␈α
papers␈αrequired
␈↓ ↓H␈↓full␈αuse␈αof␈αcommas,␈αbut␈αdue␈αto␈αa␈αprogramming␈αerror␈αin␈αthe␈αLISP␈αoutput␈αroutine,␈αlists␈αwere␈αprinted
␈↓ ↓H␈↓without commas. It looked so much better that the bug became institutionalized.
␈↓ ↓H␈↓␈↓↓2.7␈↓ π:Lists: representations of sequences     37␈↓


␈↓ ↓H␈↓S-expressions␈α
called␈αlists.␈α
 Clearly␈αthe␈α
final␈αstep␈α
is␈αto␈α
represent␈αour␈α
sequence-manipulators␈αas␈α
certain
␈↓ ↓H␈↓LISP␈αfunctions.␈α Let␈α␈↓αfirst␈↓βr␈↓␈αbe␈αa␈αLISP␈αfunction␈αwhich␈αwill␈αrepresent␈αthe␈αsequence␈αoperation␈α␈↓αfirst␈↓␈↓π 26␈↓.
␈↓ ↓H␈↓Then for example we might expect:
␈↓ ↓H␈↓α␈↓ ∧/first␈↓βr␈↓α[(A, B, C)] = ␈↓λr␈↓∞(␈↓α first[␈↓↓(A, B, C)␈↓α] ␈↓∞)␈↓α = A␈↓.

␈↓ ↓H␈↓The␈α∪problem␈α∪is␈α∪that␈α∪this␈α∪line␈α∪is␈α∪not␈α∪quite␈α∪right.␈α∪ LISP␈α∪functions␈α∪expect␈α∪their␈α∪inputs␈α∪to␈α∪be
␈↓ ↓H␈↓S-expressions but ␈↓α(A, B, C)␈↓ is ␈↓↓not␈↓ an S-expression.  To be correct we ␈↓↓should␈↓ have written:

␈↓ ↓H␈↓␈↓ ¬␈↓αfirst␈↓βr␈↓α[(A .(B . (C . NIL)))] = A␈↓β


␈↓ ↓H␈↓It␈α≤might␈α≤be␈α≤argued␈α≤that␈α≤␈↓α(A,␈α≤B,␈α≤C)␈↓␈α≤is␈α≤just␈α≤a␈α≤convenient␈α≤abbreviation␈α≤for␈α≤the␈α≠ugly
␈↓ ↓H␈↓␈↓α(A . (B . (C . NIL)))␈↓,␈αbut␈αeven␈αso,␈αif␈αwe␈αwish␈αthe␈αmachine␈αto␈αunderstand␈αand␈αuse␈αthe␈αabbreviation
␈↓ ↓H␈↓we␈α
must␈α
examine␈α
the␈α
implications␈α
of␈α
the␈α
notation.␈α
Clearly␈α
it␈α
is␈α
more␈α
reasonable␈α
to␈α
read␈α
and␈α
write␈α
in
␈↓ ↓H␈↓list␈α
notation.␈α
 As␈αlong␈α
as␈α
we␈αperform␈α
only␈α
list-operations␈αon␈α
lists␈α
there␈αis␈α
no␈α
reason␈αto␈α
look␈α
at␈αthe
␈↓ ↓H␈↓underlying␈αdotted-pair␈αrepresentation␈↓π 27␈↓.␈α However,␈αit␈αmust␈αbe␈αremembered␈αthat␈αlist␈α
operations␈αare
␈↓ ↓H␈↓carried␈α⊗out␈α⊗on␈α∃the␈α⊗machine␈α⊗using␈α∃the␈α⊗dotted-pair␈α⊗representation.␈α∃␈↓↓We␈↓␈α⊗might␈α⊗carry␈α⊗out␈α∃the
␈↓ ↓H␈↓"list-to-dotted-pair"␈α
transformations␈αimplicitly,␈α
but␈αa␈α
machine␈α
which␈αevaluates␈α
LISP␈αexpressions␈α
will
␈↓ ↓H␈↓have to have an explict transformation mechanism.

␈↓ ↓H␈↓So␈αa␈αconvenient␈αand␈α
even␈αnecessary␈αpart␈αof␈αour␈α
representation␈αof␈αsequences␈αis␈αthe␈α
specification␈αof
␈↓ ↓H␈↓transformations␈α∂between␈α∂the␈α∂abstract␈α∞data␈α∂structure␈α∂notation␈α∂and␈α∞the␈α∂notation␈α∂of␈α∂the␈α∞underlying
␈↓ ↓H␈↓representation.

␈↓ ↓H␈↓Next␈α
we␈α
can␈α
give␈α
representations␈α
for␈α∞the␈α
sequence␈α
operations.␈α
 To␈α
be␈α
precise␈α
we␈α∞should␈α
continue
␈↓ ↓H␈↓our␈αconvention␈αof␈αwriting␈αthe␈αsubscript␈α␈↓βr␈↓␈αon␈αthe␈αLISP␈αrepresentation␈αof␈αa␈αsequence␈αoperation;␈αthus
␈↓ ↓H␈↓␈↓αseq␈↓␈αis␈αrepresented␈αby␈α␈↓αseq␈↓βr␈↓.␈αIn␈αmost␈αcircumstances␈αno␈αconfusion␈αis␈αlikely,␈αso␈αwe␈αwill␈αusually␈α
omit␈αthe
␈↓ ↓H␈↓subscript.

␈↓ ↓H␈↓In␈αour␈αrepresentation,␈αthe␈αconstruction␈αof␈αa␈αsequence␈αfrom␈αan␈αarbitrary␈αnumber␈αof␈αelements␈αwill␈αbe
␈↓ ↓H␈↓represented by a LISP function ␈↓αseq␈↓βr␈↓. We will use ␈↓αlist␈↓ interchangeably with ␈↓αseq␈↓βr␈↓.
␈↓ ↓H␈↓␈↓ ¬h␈↓λr␈↓∞(␈↓α seq ␈↓∞)␈↓α = list









␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 26␈↓ Indeed, once the ␈↓λr␈↓-mapping is defined on the ␈↓↓domain␈↓ it is induced on the ␈↓↓operations␈↓.

␈↓ ↓H␈↓␈↓π 27␈↓␈α⊃Indeed,␈α∩a␈α⊃strong␈α∩case␈α⊃can␈α∩be␈α⊃made␈α⊃for␈α∩␈↓↓never␈↓␈α⊃allowing␈α∩any␈α⊃operations␈α∩on␈α⊃lists␈α∩␈↓↓except␈↓␈α⊃list
␈↓ ↓H␈↓operations! See the discussion of type-faults on page 26 and page 178.
␈↓ ↓H␈↓␈↓↓38  Symbolic expressions␈↓ 52.7␈↓



␈↓ ↓H␈↓␈↓αlist[␈↓λα␈↓β1␈↓α;␈α␈↓λα␈↓β2␈↓α;␈α...␈α
;␈↓λα␈↓βn␈↓α]␈↓␈αgenerates␈αa␈α
list␈αconsisting␈αof␈αthe␈α
␈↓λα␈↓βi␈↓␈αarguments.␈αThat␈α
is,␈α␈↓αlist␈↓␈αis␈α
the␈αappropriately
␈↓ ↓H␈↓nested composition of ␈↓αcons␈↓es:

␈↓ ↓H␈↓␈↓ ∧a␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α; ... cons[␈↓λα␈↓βn␈↓α;NIL]] ...] ␈↓. 
␈↓ ↓H␈↓Examples:
␈↓ ↓H␈↓α␈↓ ¬blist[A;B] = (A B)
␈↓ ↓H␈↓α␈↓ ¬Flist[A;B;C] = (A B C)
␈↓ ↓H␈↓α␈↓ ∧Olist[cons[A;B];car[(A . B)]] = ((A . B) A)
␈↓ ↓H␈↓α␈↓ ∧:list[A;list[B;C]] = list[A;(B C)] = (A (B C))
␈↓ ↓H␈↓α␈↓ ε∀list[] = ()
␈↓ ↓H␈↓α␈↓ ¬Ylist[NIL] = (NIL)

␈↓ ↓H␈↓Notice␈α
that␈α
␈↓αlist␈↓␈α
is␈α␈↓↓not␈↓␈α
strictly␈α
a␈α
function␈α
in␈αthe␈α
LISP␈α
sense;␈α
it␈α
␈↓↓does␈↓␈αevaluate␈α
its␈α
arguments,␈α
but␈αit␈α
can
␈↓ ↓H␈↓take␈α⊃an␈α∩arbitrary␈α⊃number␈α∩of␈α⊃them.␈α⊃See␈α∩page 21.␈α⊃ For␈α∩the␈α⊃moment,␈α⊃␈↓αlist␈↓␈α∩is␈α⊃simply␈α∩a␈α⊃notational
␈↓ ↓H␈↓abbreviation␈α
for␈αnested␈α
applications␈αof␈α
␈↓αcons␈↓.␈α The␈α
representation␈αof␈α
the␈αselector␈α
functions␈αshould␈α
be
␈↓ ↓H␈↓apparent␈α∞from␈α∞the␈α∞graphical␈α∂representation.␈α∞We␈α∞leave␈α∞it␈α∞as␈α∂an␈α∞exercise␈α∞for␈α∞the␈α∞reader␈α∂to␈α∞specify
␈↓ ↓H␈↓representations for these functions; however, here are a few of the other representations:
␈↓ ↓H␈↓␈↓ ¬F␈↓λr␈↓∞(␈↓α isindiv ␈↓∞)␈↓α = atom

␈↓ ↓H␈↓α␈↓ ∧␈␈↓λr␈↓∞(␈↓α isseq ␈↓∞)␈↓α = isstrictlist␈↓  where:

␈↓ ↓H␈↓α␈↓ αxisstrictlist[x] <=␈↓ ∧x[atom[x] → [eq[x; NIL] → ␈↓
t␈↓α; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ αx␈↓ ∧x islistelement[car[x]] → isstrictlist[cdr[x]];
␈↓ ↓H␈↓α␈↓ αx␈↓ ∧x ␈↓
t␈↓α → ␈↓
f␈↓α]

␈↓ ↓H␈↓α␈↓ αxislistelement[x] <=␈↓ ∧x[atom[x] → ␈↓
t␈↓α;   ␈↓
t␈↓α → isstrictlist[x]]

␈↓ ↓H␈↓Some␈α∞practicioners␈α∞of␈α∞LISP␈α∞use␈α∞this␈α∞strict␈α∞definition␈α∞of␈α∞lists,␈α∞so␈α∞that␈α∞elements␈α∞of␈α∞a␈α∞list␈α∞are␈α
either
␈↓ ↓H␈↓atomic␈α∞or␈α
are␈α∞lists␈α
themselves.␈α∞ In␈α
practice␈α∞it␈α∞is␈α
often␈α∞convenient␈α
to␈α∞allow␈α
elements␈α∞of␈α
a␈α∞list␈α∞to␈α
be
␈↓ ↓H␈↓arbitrary␈α∩S-expressions.␈α∩This␈α⊃more␈α∩liberal␈α∩interpretation␈α⊃of␈α∩lists␈α∩is␈α⊃expressed␈α∩by␈α∩the␈α⊃following
␈↓ ↓H␈↓recognizer:

␈↓ ↓H␈↓␈↓ βa␈↓αislist[x] <= [atom[x] →[eq[x;NIL]→␈↓
t␈↓α;␈↓
t␈↓α→␈↓
f␈↓α]; ␈↓
t␈↓α→islist[cdr[x]] ]

␈↓ ↓H␈↓Thus ␈↓α(A, (A . B), C)␈↓ is a list of three elements.  But beware: ␈↓↓(A, (A . B), C)␈↓ is ␈↓↓not␈↓ a sequence.

␈↓ ↓H␈↓To␈αsummarize␈α
the␈αaccomplishments␈α
of␈αthis␈α
section,␈αwe␈αhave␈α
in␈αeffect␈α
added␈αa␈α
␈↓↓new␈↓␈αdata␈αstructure␈α
to
␈↓ ↓H␈↓the repertoire of LISP.  The addition process included:

␈↓ ↓H␈↓␈↓↓1.␈α⊂ The␈α⊃abstract␈α⊂operations.␈↓␈α⊃We␈α⊂give␈α⊃constructors,␈α⊂selectors,␈α⊃and␈α⊂predicates␈α⊃for␈α⊂the
␈↓ ↓H␈↓␈↓ αhrecognition of instances of the data structure.

␈↓ ↓H␈↓␈↓↓2.␈α The␈αunderlying␈αrepresentation.␈↓␈αWe␈αmust␈αshow␈αhow␈αthe␈αnew␈αdata␈αstructure␈αcan␈αbe
␈↓ ↓H␈↓␈↓ αhrepresented in terms of existing data structures.
␈↓ ↓H␈↓␈↓↓2.7␈↓ π:Lists: representations of sequences     39␈↓


␈↓ ↓H␈↓␈↓↓3.␈α Abstract␈α
operations␈αas␈αconcrete␈α
operations.␈↓␈αWe␈αmust␈α
write␈αLISP␈α
functions␈αwhich
␈↓ ↓H␈↓␈↓ αhfaithfully␈α⊂mirror␈α⊂the␈α⊂intended␈α∂meaning␈α⊂of␈α⊂the␈α⊂abstract␈α⊂operations␈α∂when
␈↓ ↓H␈↓␈↓ αhinterpreted in the underlying representation.

␈↓ ↓H␈↓␈↓↓4.␈α The␈α
input/output␈αtransformations.␈↓␈α
We␈αshould␈α
give␈αconventions␈α
for␈αtransforming
␈↓ ↓H␈↓␈↓ αhto and from the internal representation.

␈↓ ↓H␈↓There␈αis␈αanother␈αview␈αof␈αthis␈αproblem␈αof␈αrepresentability␈αof␈αdata␈αstructures␈αdue␈αto␈αJ.␈αMorris.␈αHere
␈↓ ↓H␈↓we␈αuse␈αthe␈α
notion␈αof␈α␈↓↓transfer␈αfunctions␈↓␈α
whose␈αpurpose␈αis␈αto␈α
supply␈αmappings␈αbetween␈αthe␈α
abstract
␈↓ ↓H␈↓structure␈αand␈αits␈αrepresentation.␈α Once␈αthe␈αtransfer␈α
functions␈αare␈αgiven␈αit␈αis␈αusually␈αeasy␈α
to␈αsupply
␈↓ ↓H␈↓appropriate␈α⊗implementations␈α⊗of␈α↔the␈α⊗abstract␈α⊗primitives.␈α⊗We␈α↔need␈α⊗two␈α⊗transfer␈α↔functions;␈α⊗a
␈↓ ↓H␈↓write-function,␈α
␈↓	W␈↓,␈α∞to␈α
map␈α∞the␈α
representations␈α∞into␈α
the␈α
abstract␈α∞objects;␈α
and␈α∞a␈α
read-function,␈α∞␈↓	R␈↓,␈α
to
␈↓ ↓H␈↓map the abstract objects to their representations.

␈↓ ↓H␈↓For␈α
the␈α
problem␈αat␈α
hand,␈α
representing␈α
sequences,␈αwe␈α
want␈α
␈↓	R␈↓␈α
to␈αmap␈α
from␈α
elements␈α
of␈α␈↓<seq␈α
elem>␈↓
␈↓ ↓H␈↓to␈α
␈↓<sexpr>␈↓␈α∞(see␈α
page 30␈α∞and␈α
page 10);␈α∞and␈α
we␈α
want␈α∞␈↓	W␈↓␈α
to␈α∞map␈α
from␈α∞␈↓<sexpr>␈↓␈α
to␈α∞␈↓<seq␈α
elem>␈↓.
␈↓ ↓H␈↓Before we give such ␈↓	R␈↓ and ␈↓	W␈↓, let's see what they will do for us.  We could define ␈↓αfirst␈↓βr␈↓ such that:
␈↓ ↓H␈↓␈↓ ¬:␈↓αfirst␈↓βr␈↓α[x] = ␈↓	W␈↓α(car[␈↓	R␈↓α(x)])

␈↓ ↓H␈↓Here␈α∂we␈α∂have␈α∂used␈α∂"(...)"␈α∞for␈α∂function␈α∂application␈α∂rather␈α∂than␈α∞"[...]"␈α∂which␈α∂is␈α∂reserved␈α∂for␈α∞LISP
␈↓ ↓H␈↓evaluation.␈αWhat␈α
the␈αequation␈α
says␈αis␈αthat␈α
given␈αa␈α
sequence␈α␈↓αx␈↓,␈αwe␈α
can␈αmap␈α
it␈αto␈α
the␈αS-expression
␈↓ ↓H␈↓representation␈α∂using␈α∞␈↓	R␈↓;␈α∂the␈α∞result␈α∂of␈α∞this␈α∂map␈α∞is␈α∂an␈α∞S-expr␈α∂and␈α∞therefore␈α∂suitable␈α∞fare␈α∂for␈α∞␈↓αcar␈↓'s
␈↓ ↓H␈↓delicate␈α
palate;␈α
the␈α
result␈α
of␈α
the␈α
␈↓αcar␈↓␈α
operation␈α
is␈α
then␈α
mapped␈α
back␈α
into␈α
the␈α
set␈α
of␈αsequence␈α
elements
␈↓ ↓H␈↓by␈α⊂␈↓	W␈↓.␈α∂ The␈α⊂other␈α⊂operations␈α∂for␈α⊂manipulating␈α∂sequences␈α⊂can␈α⊂be␈α∂described␈α⊂similarly.␈α⊂ With␈α∂this
␈↓ ↓H␈↓introduction, here are appropriate transfer functions:

␈↓ ↓H␈↓α␈↓	W␈↓α(e) <=␈↓ β([isnil[e] → mknull[];
␈↓ ↓H␈↓α␈↓ β( atom[e] → mkindiv[e];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → concat[␈↓	W␈↓α(car[e]);␈↓	W␈↓α(cdr[e])] ]

␈↓ ↓H␈↓α␈↓	R␈↓α(l) <=␈↓ β([null[l] → NIL;
␈↓ ↓H␈↓α␈↓ β( isindiv[l] → atomize[l];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → cons[␈↓	R␈↓α(first[l]);␈↓	R␈↓α(rest[l])] ]


␈↓ ↓H␈↓We␈α
have␈α
seen␈α
all␈α
of␈α∞the␈α
functions␈α
and␈α
predicates␈α
involved␈α
in␈α∞␈↓	R␈↓␈α
and␈α
␈↓	W␈↓␈α
except␈α
the␈α∞three␈α
␈↓αatomize␈↓,
␈↓ ↓H␈↓␈↓αmknull␈↓␈αand␈α␈↓αmkindiv␈↓.␈αIf␈αyou␈αthink␈αabout␈αthe␈αimplementation␈αof␈αthese␈αthree␈αfunctions␈αyou␈αwould␈αsee
␈↓ ↓H␈↓that␈αthey␈αare␈α
the␈αidentity␈αfunctions␈α
for␈αall␈αpractical␈α
purposes.␈αHowever␈αthey␈α
␈↓↓are␈↓␈αonly␈αbecause␈αof␈α
the
␈↓ ↓H␈↓representations␈α
that␈α
we␈α
happened␈α
to␈α
pick;␈α
they␈αneed␈α
not␈α
be␈α
so␈α
simple.␈α
 A␈α
more␈α
careful␈αinspection
␈↓ ↓H␈↓would␈α∀show␈α∀that␈α∀␈↓αmkindiv␈↓␈α∃expects␈α∀as␈α∀input␈α∀an␈α∃atomic␈α∀S-expression␈α∀and␈α∀outputs␈α∃a␈α∀sequence
␈↓ ↓H␈↓individual;␈α␈↓αatomize␈↓␈αacts␈αconversely.␈α
If␈αthe␈αrepresentations␈αof␈α
the␈αatomic␈αS-expressions␈αwere␈α
different
␈↓ ↓H␈↓from the representations of sequence individuals, then we would have some work to do.

␈↓ ↓H␈↓The␈αscheme␈αof␈αtransfer␈αfunctions␈αis␈αa␈αmore␈αmathematical␈αapproach␈αto␈αthat␈αoutlined␈αabove␈αin␈α␈↓↓1.␈↓-␈↓↓4.␈↓.
␈↓ ↓H␈↓We␈α∞find␈α
␈↓↓1.␈↓-␈↓↓4.␈↓␈α∞preferable␈α∞since␈α
it␈α∞gives␈α∞a␈α
description␈α∞more␈α∞in␈α
line␈α∞with␈α∞what␈α
we␈α∞should␈α∞expect␈α
to
␈↓ ↓H␈↓implement in a programming language.
␈↓ ↓H␈↓␈↓↓40  Symbolic expressions␈↓ 52.7␈↓


␈↓ ↓H␈↓To␈αfinally␈αreview␈αwhat␈αhas␈αtranspired␈αsince␈αit␈αis␈αa␈αmodel␈αof␈αwhat␈αis␈αto␈αcome:␈αwe␈αdeveloped␈αa␈αnew
␈↓ ↓H␈↓abstract␈α⊃data␈α⊂structure␈α⊃called␈α⊃sequences;␈α⊂discussed␈α⊃notational␈α⊂conventions␈α⊃for␈α⊃writing␈α⊂sequences;
␈↓ ↓H␈↓described␈α
operations␈α
and␈α
pertinent␈α
control␈α
devices␈α
for␈α
writing␈α
algorithms;␈α
and␈α
finally␈α
showed␈α
that␈α
it
␈↓ ↓H␈↓was␈α
possible␈αto␈α
represent␈αsequences␈α
in␈α
the␈αpreviously␈α
developed␈αdomain␈α
of␈α
S-exprs.␈αThus␈α
if␈αwe␈α
had
␈↓ ↓H␈↓a␈α∞machine␈α∞which␈α∞could␈α
execute␈α∞S-expr␈α∞algorithms␈α∞we␈α
could␈α∞encapsulate␈α∞that␈α∞machine␈α∞within␈α
the
␈↓ ↓H␈↓␈↓λr␈↓-mapping␈α⊃such␈α⊂that␈α⊃we␈α⊃could␈α⊂write␈α⊃in␈α⊂sequence-notation␈α⊃and␈α⊃have␈α⊂it␈α⊃translated␈α⊃internally␈α⊂to
␈↓ ↓H␈↓S-expr␈α
form;␈α
we␈α
could␈α
write␈α
sequence-algorithms␈α
and␈α
have␈α
them␈α
execute␈α
correctly␈α
using␈αthe␈α
␈↓λr␈↓-maps
␈↓ ↓H␈↓of␈α
the␈α
sequence␈α
primitives;␈α
and␈α
finally␈αit␈α
would␈α
produce␈α
sequence-output␈α
rather␈α
than␈α
the␈αinternal
␈↓ ↓H␈↓S-expr␈α∂form.␈α∂To␈α∂all␈α∂intents␈α∂and␈α∂purposes␈α∂our␈α∂augmented␈α∂LISP␈α∂machine␈α⊂understands␈α∂sequences.
␈↓ ↓H␈↓Indeed,␈α
this␈α
is␈α∞the␈α
way␈α
most␈α
LISP␈α∞implementations␈α
are␈α
organized;␈α
input␈α∞may␈α
either␈α
be␈α∞in␈α
S-expr
␈↓ ↓H␈↓form␈α
or␈α
list-notation;␈α
internally␈αall␈α
data␈α
structures␈α
are␈α
stored␈αas␈α
S-exprs;␈α
all␈α
algorithms␈α
operate␈αon
␈↓ ↓H␈↓the S-expr form; and finally, S-exprs which can be interpreted as lists are output in list-notation.

␈↓ ↓H␈↓We␈αwill␈αapproach␈αthe␈αother␈αabstract␈αdata␈αstructure␈αproblems␈αin␈αa␈αsimilar␈αmanner,␈αfirst␈αdeveloping
␈↓ ↓H␈↓the␈α∂data␈α∞structures␈α∂independent␈α∞of␈α∂their␈α∞representation,␈α∂and␈α∞later␈α∂showing␈α∞how␈α∂to␈α∂represent␈α∞this
␈↓ ↓H␈↓new␈α⊃domain␈α∩in␈α⊃terms␈α⊃of␈α∩some␈α⊃previously␈α⊃understood␈α∩domain.␈α⊃We␈α⊃will␈α∩see␈α⊃in␈α∩Section 6.5␈α⊃that
␈↓ ↓H␈↓much␈αof␈αthe␈αmapping␈αfrom␈αinput␈αthrough␈αoutput␈αcan␈αbe␈αspecified␈αin␈αa␈αnatural␈αstyle␈αand␈αLISP␈αcan
␈↓ ↓H␈↓automatically generate the necessary input and output programs.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I␈α∞Discuss␈α∞␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α;␈↓λα␈↓β3␈↓α]]␈↓␈α∂as␈α∞opposed␈α∞to␈α∞␈↓αcons[␈↓λα␈↓β1␈↓α;cons[␈↓λα␈↓β2␈↓α;␈α∂cons[␈↓λα␈↓β3␈↓α;␈α∞NIL]]]␈↓␈α∞as␈α∂a␈α∞representation
␈↓ ↓H␈↓for ␈↓α(␈↓λα␈↓β1␈↓α, ␈↓λα␈↓β2␈↓α, ␈↓λα␈↓β3␈↓α)␈↓.



␈↓ ↓H␈↓␈↓ ∧v␈↓↓Problems involving list-notation␈↓

␈↓ ↓H␈↓I  Translate the following lists into S-expr dotted-pair notation.
␈↓ ↓H␈↓␈↓ βo␈↓↓1.␈↓α (A B C)   ␈↓↓2.␈↓α (A)   ␈↓↓3.␈↓α ((A))   ␈↓↓4.␈↓α (A (B (C)))  ␈↓↓5.␈↓α (NIL).

␈↓ ↓H␈↓Now go the other way and translate the following S-exprs into list notation.
␈↓ ↓H␈↓␈↓ βu␈↓↓6.␈↓α ((A .(B . NIL)).((C . NIL). NIL))   ␈↓↓7.␈↓α (NIL . NIL)
␈↓ ↓H␈↓α␈↓ ∧L␈↓↓8.␈↓α (CONS .((QUOTE .(A . NIL)). NIL))

␈↓ ↓H␈↓II  Evaluate the following:
␈↓ ↓H␈↓␈↓ ¬∀␈↓↓1.␈↓α first[(A B)]   ␈↓↓2.␈↓α rest[(A B)]
␈↓ ↓H␈↓α␈↓ ∧b␈↓↓3.␈↓α concat[A;(B C)]   ␈↓↓4.␈↓α concat[A;NIL]
␈↓ ↓H␈↓α␈↓ ∧+␈↓↓5.␈↓α concat[eq[A;A];(A B C)] ␈↓↓6.␈↓α first[rest[(A B)]]
␈↓ ↓H␈↓␈↓↓2.8␈↓ 
∂A respite     41␈↓


␈↓ ↓H␈↓␈↓ ¬w␈↓↓2.8  A respite␈↓


␈↓ ↓H␈↓This␈α∂section␈α∞contains␈α∂some␈α∂hints␈α∞and␈α∂notes␈α∞on␈α∂the␈α∂introductory␈α∞material␈α∂of␈α∞this␈α∂chapter.␈α∂First␈α∞a
␈↓ ↓H␈↓reiteration␈αof␈αa␈αprevious␈αadmonition:␈αthough␈αmost␈αof␈αthis␈αmaterial␈αseems␈αquite␈αstraightforward,␈αthe
␈↓ ↓H␈↓next␈α∞chapter␈α
will␈α∞begin␈α
to␈α∞show␈α∞you␈α
that␈α∞things␈α
are␈α∞not␈α∞all␈α
that␈α∞trivial.␈α
 LISP␈α∞is␈α∞quite␈α
powerful.
␈↓ ↓H␈↓The preceding material ␈↓↓is␈↓ basic and the sooner it becomes second nature to you the better.

␈↓ ↓H␈↓A␈α∂second␈α∂admonition:␈α∂besides␈α∂learning␈α∂about␈α⊂the␈α∂basic␈α∂constructs␈α∂of␈α∂the␈α∂language,␈α⊂the␈α∂previous
␈↓ ↓H␈↓material␈α∂should␈α∂begin␈α∂to␈α∞convince␈α∂you␈α∂of␈α∂the␈α∞necessity␈α∂for␈α∂precise␈α∂specification␈α∂of␈α∞programming
␈↓ ↓H␈↓languages.␈αIn␈αparticular␈αwe␈αhave␈αseen␈αthat␈αthe␈αprocess␈αof␈αevaluation␈αof␈αexpressions␈αmust␈αbe␈αspelled
␈↓ ↓H␈↓out␈αquite␈αcarefully.␈αDifferent␈αevaluation␈αschemes␈αlead␈αto␈αquite␈αdifferent␈αprograms.␈αSince␈αevaluation
␈↓ ↓H␈↓␈↓↓is␈↓ the business of programming languages we'd better do all we can to make a precise specification.

␈↓ ↓H␈↓And␈α∪a␈α∪final␈α∪warning:␈α∩a␈α∪major␈α∪point␈α∪of␈α∩this␈α∪whole␈α∪book␈α∪is␈α∩to␈α∪stress␈α∪the␈α∪proper␈α∪respect␈α∩for
␈↓ ↓H␈↓abstraction␈α∪as␈α∩a␈α∪tool␈α∩for␈α∪controlling␈α∩complexity␈α∪in␈α∩programming,␈α∪and␈α∩as␈α∪a␈α∩means␈α∪of␈α∩writing
␈↓ ↓H␈↓implementation␈α↔(representation)␈α↔independent␈α↔programs.␈α_As␈α↔we␈α↔begin␈α↔writing␈α_more␈α↔complex
␈↓ ↓H␈↓algorithms,␈α⊂the␈α⊂power␈α∂of␈α⊂abstraction␈α⊂will␈α⊂become␈α∂more␈α⊂apparent,␈α⊂but␈α∂the␈α⊂lessons␈α⊂we␈α⊂learned␈α∂in
␈↓ ↓H␈↓representing␈αsequences␈αcontain␈αthe␈αessential␈αideas␈αof␈αabstraction␈αand␈αrepresentation.␈α Do␈αnot␈αforget
␈↓ ↓H␈↓them.

␈↓ ↓H␈↓We␈α∃have␈α∃now␈α∃seen␈α∃two␈α∃examples␈α∃of␈α∃abstract␈α∃data␈α∃structures.␈α∃First,␈α∃the␈α∃study␈α∃of␈α∀Symbolic
␈↓ ↓H␈↓Expressions␈α∂was␈α∂begun␈α∂without␈α∂any␈α∂regard␈α⊂for␈α∂their␈α∂implementation.␈α∂ They␈α∂were␈α∂deemed␈α⊂to␈α∂be
␈↓ ↓H␈↓objects␈α
of␈αsufficient␈α
interest␈α
in␈αtheir␈α
own␈αright␈↓π 28␈↓.␈α
The␈α
basic␈αcomponents␈α
of␈α
our␈αstudy␈α
were␈αthe␈α
data
␈↓ ↓H␈↓structures,␈αthemselves;␈αthe␈αoperations␈αon␈αthe␈αdata␈αstructures␈α(␈↓αcar,␈αcdr,␈αcons,␈αeq␈↓␈αand␈α␈↓αatom␈↓),␈αand␈αfinally
␈↓ ↓H␈↓the␈α
␈↓↓control␈αstructures␈↓␈α
needed␈αby␈α
the␈αalgorithms␈α
which␈α
operated␈αon␈α
the␈αdata␈α
structures.␈αThe␈α
control
␈↓ ↓H␈↓structures␈α
for␈α∞our␈α
LISP␈α∞algorithms␈α
are␈α∞the␈α
conditional␈α∞expression␈α
and␈α∞recursion.␈α
They␈α∞are␈α
called
␈↓ ↓H␈↓control␈α
structures␈α
since␈α
they␈α
are␈α
used␈α
to␈α
direct␈αthe␈α
flow␈α
of␈α
the␈α
algorithm␈α
as␈α
it␈α
executes.␈α Indeed␈α
these
␈↓ ↓H␈↓three␈α⊂components,␈α⊃data,␈α⊂operations,␈α⊃and␈α⊂control,␈α⊂are␈α⊃the␈α⊂main␈α⊃ingredients␈α⊂of␈α⊃any␈α⊂programming
␈↓ ↓H␈↓language.␈α∂ Most␈α∂languages␈α∂have␈α∂a␈α∂superficially␈α∂richer␈α∂class␈α∂of␈α∂control␈α∂devices;␈α∂"while"-statements
␈↓ ↓H␈↓and␈α⊂"DO"-loops␈α⊂are␈α⊃examples.␈α⊂Most␈α⊂control␈α⊂structures␈α⊃are␈α⊂explicit␈α⊂language␈α⊃constructs,␈α⊂whereas
␈↓ ↓H␈↓recursion is typically implicit␈↓π 29␈↓.

␈↓ ↓H␈↓As␈α
we␈α
introduce␈α
each␈α
new␈α
abstract␈α
data␈α
structure␈αwe␈α
add␈α
new␈α
operations␈α
tailored␈α
to␈α
its␈α
needs.␈αIn
␈↓ ↓H␈↓adding␈α∩sequences␈α∩we␈α∩introduced␈α∩␈↓αfirst,␈α∩rest,␈α∪null, ...␈↓.␈α∩ We␈α∩should␈α∩also␈α∩consider␈α∩the␈α∪question␈α∩of
␈↓ ↓H␈↓introducing␈α∀new␈α∪control␈α∀structures.␈α∀ Again,␈α∪with␈α∀sequences␈α∀we␈α∪stayed␈α∀with␈α∀recursion,␈α∪though
␈↓ ↓H␈↓perhaps␈α∂a␈α∂simpler␈α∂control␈α∂structure␈α∂which␈α⊂went␈α∂down␈α∂the␈α∂sequence,␈α∂selecting␈α∂elements␈α⊂might␈α∂be
␈↓ ↓H␈↓more␈α∞in␈α∞keeping␈α∞with␈α∞the␈α∞data␈α∞structure.␈α
 There␈α∞is␈α∞a␈α∞natural␈α∞relationship␈α∞between␈α∞data␈α
structure
␈↓ ↓H␈↓and␈αcontrol␈αstructure;␈αsometimes␈αwe␈αcan␈αexploit␈αit␈αto␈αgood␈αmeasure.␈αLooking␈αahead,␈αthe␈αiterator␈α␈↓αlit␈↓

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 28␈↓␈α
But␈α
if␈αthere␈α
is␈α
some␈αnagging␈α
doubt␈α
about␈αthe␈α
problems␈α
of␈αimplementation,␈α
relax;␈α
we'll␈αsee␈α
plenty
␈↓ ↓H␈↓of this kind of thing later.

␈↓ ↓H␈↓␈↓π 29␈↓␈αHowever␈αsome␈αlanguages␈αdo␈α
require␈αsome␈αkind␈αof␈αdeclaration␈α
to␈αthe␈αeffect␈αthat␈αa␈α
procedure␈αis
␈↓ ↓H␈↓recursive.
␈↓ ↓H␈↓␈↓↓42  Symbolic expressions␈↓ 42.8␈↓


␈↓ ↓H␈↓on␈αpage 146␈αis␈α
an␈αexample␈αof␈αa␈α
control␈αregime␈αapplicable␈αto␈α
sequences.␈α When␈αwe␈αconsider␈α
abstract
␈↓ ↓H␈↓data␈α∂structures␈α∂in␈α∞future␈α∂chapters␈α∂we␈α∂will␈α∞again␈α∂see␈α∂the␈α∞three␈α∂components:␈α∂data,␈α∂operations,␈α∞and
␈↓ ↓H␈↓control.

␈↓ ↓H␈↓The␈α
new␈α
feature␈α
which␈α
we␈α
considered␈αin␈α
discussing␈α
sequences␈α
was␈α
the␈α
problem␈α
of␈αrepresentation.
␈↓ ↓H␈↓We␈α
showed␈α
how␈αto␈α
represent␈α
sequences␈αin␈α
terms␈α
of␈α
S-expressions.␈αWe␈α
will␈α
continue␈αthis␈α
pyramiding
␈↓ ↓H␈↓of␈α
data␈α
structures␈α
in␈α
the␈α
future;␈α
we␈α
will␈α
consider␈α
our␈α
work␈α
done␈α
as␈α
soon␈α
as␈α
we␈α
have␈α
a␈α
representation
␈↓ ↓H␈↓of␈α
our␈α
new␈α
data␈α
structure␈α
in␈α
terms␈α
of␈α
an␈α
existing␈α
one.␈α
Finally␈α
the␈α
suspense␈α
will␈α
become␈α
too␈α
great
␈↓ ↓H␈↓and␈α∞we␈α
will␈α∞exhibit␈α
a␈α∞representation␈α
of␈α∞the␈α
underlying␈α∞layer␈α
of␈α∞S-expressions.␈α
Even␈α∞later␈α∞we␈α
will
␈↓ ↓H␈↓discuss␈α∀efficient␈α∀representation␈α∃of␈α∀data␈α∀structures,␈α∃independent␈α∀of␈α∀their␈α∃possible␈α∀S-expression
␈↓ ↓H␈↓representation.␈α∃Indeed,␈α⊗there␈α∃are␈α⊗lots␈α∃of␈α⊗data␈α∃structures␈α⊗which␈α∃are␈α⊗not␈α∃best␈α⊗represented␈α∃as
␈↓ ↓H␈↓S-expressions.␈α A␈αfurther␈αconsideration␈αappears␈αbecause␈αof␈αthe␈αrepresentational␈αissue;␈α
even␈αthough
␈↓ ↓H␈↓we␈α∞have␈α∞represented␈α∞a␈α∂particular␈α∞data␈α∞structure␈α∞as␈α∞a␈α∂complex␈α∞S-expression␈α∞we␈α∞have␈α∂␈↓↓no␈↓␈α∞business
␈↓ ↓H␈↓operating␈α
on␈αthat␈α
representation␈αwith␈α
S-expression␈α
functions.␈αPlease␈α
don't␈αuse␈α
␈↓αcar␈↓␈αand␈α
␈↓αcdr␈↓␈α
on␈αlists
␈↓ ↓H␈↓even␈α⊗though␈α∃you␈α⊗know␈α⊗what␈α∃the␈α⊗representation␈α⊗is.␈α∃ You␈α⊗might␈α⊗have␈α∃noticed␈α⊗that␈α⊗in␈α∃our
␈↓ ↓H␈↓representation␈αof␈αlists␈αwe␈αcan␈αfind␈αthe␈αn␈↓πth␈↓␈αelement␈αin␈αa␈αlist␈αby␈αusing␈α␈↓αcad␈↓πn-1␈↓αr␈↓.␈αAnd␈αyou␈αknow␈α␈↓αcadr␈↓␈αis
␈↓ ↓H␈↓the␈α∪second␈α∩element,␈α∪and␈α∩that␈α∪␈↓αcdr␈↓␈α∩is␈α∪the␈α∩␈↓αrest␈↓␈α∪of␈α∩the␈α∪list.␈α∩ But␈α∪please␈α∩keep␈α∪it␈α∩a␈α∪secret.␈α∩These
␈↓ ↓H␈↓representation-dependent␈αcoding␈αtricks␈↓π 30␈↓␈α
are␈αdangerous.␈αThey␈α
are␈αreally␈αtype␈α
faults␈αas␈αdiscussed␈α
on
␈↓ ↓H␈↓page 26 and page 178.

␈↓ ↓H␈↓While␈αwe␈αare␈αdiscussing␈αsome␈αof␈αthe␈αmore␈αpractical␈αimplications␈αof␈αour␈αwork␈αwe␈αshould␈αnot␈α
neglect
␈↓ ↓H␈↓␈↓λB␈↓.␈α∪How␈α∩should␈α∪the␈α∪be␈α∩understood.␈α∪ As␈α∩things␈α∪currently␈α∪stand,␈α∩the␈α∪appearance␈α∩of␈α∪␈↓λB␈↓␈α∪in␈α∩any
␈↓ ↓H␈↓application␈α⊃of␈α∩strict␈α⊃functions␈α∩will␈α⊃immediately␈α⊃cause␈α∩the␈α⊃termination␈α∩of␈α⊃the␈α∩computation.␈α⊃ No
␈↓ ↓H␈↓information␈α
other␈α
than␈α
the␈α
fact␈α
that␈α
␈↓λB␈↓␈α
did␈α
appear␈α
results␈α
from␈α
such␈α
an␈α
occurrence.␈α
Indeed␈α
if␈αwe
␈↓ ↓H␈↓thought␈αof␈αthe␈αevaluation␈αof␈α␈↓λB␈↓␈αas␈αresulting␈αin␈αa␈αdivergent␈αcomputation,␈αthen␈αno␈αinformation␈αat␈αall
␈↓ ↓H␈↓would␈α⊃be␈α∩forthcoming.␈α⊃ In␈α∩reality␈α⊃a␈α⊃LISP␈α∩implementation␈α⊃will␈α∩handle␈α⊃computations␈α∩which␈α⊃we
␈↓ ↓H␈↓include␈α
within␈α
the␈αprovince␈α
of␈α
␈↓λB␈↓␈α
as␈αerror␈α
conditions.␈α
The␈α
computation␈αmight␈α
be␈α
terminated␈αand␈α
an
␈↓ ↓H␈↓error␈α
printed;␈α
in␈α
an␈αinteractive␈α
implementation␈α
the␈α
user␈α
might␈αbe␈α
given␈α
an␈α
opportunity␈α
to␈αcorrect
␈↓ ↓H␈↓ther␈α_error␈α_and␈α_the␈α_computation␈α→continued;␈α_and␈α_alas,␈α_some␈α_implementations␈α→just␈α_continue
␈↓ ↓H␈↓computation␈α⊗with␈α⊗some␈α⊗arbitrary␈α⊗piece␈α⊗of␈α⊗information␈α⊗produced␈α⊗by␈α⊗an␈α⊗excursion␈α⊗into␈α∃the
␈↓ ↓H␈↓subsconscious␈α∪of␈α∪LISP.␈α∩We␈α∪will␈α∪have␈α∪much␈α∩more␈α∪to␈α∪say␈α∪about␈α∩the␈α∪implementation␈α∪of␈α∪␈↓λB␈↓␈α∩in
␈↓ ↓H␈↓Section .

␈↓ ↓H␈↓Well,␈αwhat␈αdoes␈αall␈αthis␈αhave␈αto␈αdo␈αwith␈α
a␈αcourse␈αin␈αdata␈αstructures?␈α We␈αwill␈αshow␈αquite␈αsoon␈α
that
␈↓ ↓H␈↓we␈α⊃can␈α⊃exploit␈α∩abstraction␈α⊃as␈α⊃a␈α⊃means␈α∩for␈α⊃giving␈α⊃a␈α⊃clear␈α∩specification␈α⊃of␈α⊃evaluation␈α∩of␈α⊃LISP
␈↓ ↓H␈↓expressions,␈α∞and␈α∞the␈α∂representational␈α∞techniques␈α∞we␈α∞will␈α∂use␈α∞will␈α∞involve␈α∞applications␈α∂of␈α∞abstract
␈↓ ↓H␈↓data␈αstructures.␈αA␈αmore␈αtangible␈αbenefit␈αperhaps␈αshould␈αbe␈αan␈αincreased␈αawareness␈αof␈αthe␈αstructure
␈↓ ↓H␈↓and␈α∪behavior␈α∀of␈α∪programming␈α∀languages,␈α∪and␈α∀hopefully␈α∪the␈α∀beginnings␈α∪of␈α∀a␈α∪better␈α∀style␈α∪of
␈↓ ↓H␈↓programming.

␈↓ ↓H␈↓Another␈α
part␈α
of␈α
our␈α
investigation␈αshould␈α
be␈α
to␈α
answer␈α
the␈αquestion␈α
"What␈α
is␈α
a␈α
data␈αstructure?".␈α
 As
␈↓ ↓H␈↓we␈α⊂mentioned␈α⊂at␈α∂the␈α⊂beginning␈α⊂of␈α∂Section 2.6␈α⊂there␈α⊂is␈α∂a␈α⊂different␈α⊂characterization␈α⊂of␈α∂sequences

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 30␈↓ called "puns" by C. Strachey
␈↓ ↓H␈↓␈↓↓2.8␈↓ 
∂A respite     43␈↓


␈↓ ↓H␈↓which␈αwill␈αgive␈αa␈αdifferent␈αinterpretation␈αof␈αdata␈αstructures.␈αThe␈αstandard␈αmathematical␈αdefinition
␈↓ ↓H␈↓of␈αa␈αsequence␈αis␈αas␈αa␈αfunction␈αfrom␈αthe␈αintegers␈αto␈αa␈αparticular␈αdomain.␈α Thus␈αa␈αfinite␈αsequence␈α␈↓s␈↓
␈↓ ↓H␈↓might be given as:
␈↓ ↓H␈↓␈↓ ∧m␈↓s␈↓ = ␈↓{<1, s␈↓β1␈↓>, <2, s␈↓β2␈↓>, ...<n, s␈↓βn␈↓>}

␈↓ ↓H␈↓To␈α⊂select␈α∂components␈α⊂of␈α∂␈↓s␈↓,␈α⊂we␈α∂use␈α⊂ordinary␈α∂function␈α⊂application:␈α∂␈↓s(i)␈↓ = ␈↓s␈↓βi␈↓.␈α⊂Indeed,␈α∂if␈α⊂you␈α∂have
␈↓ ↓H␈↓programmed␈αin␈αa␈αlanguage␈αwhich␈αhas␈αarray␈αconstructs,␈αyou␈αwill␈αrecognize␈α"application"␈αas␈αthe␈αstyle
␈↓ ↓H␈↓of␈α
notation␈α∞used.␈α
 However␈α
this␈α∞is␈α
quite␈α∞different␈α
from␈α
what␈α∞we␈α
did␈α
in␈α∞the␈α
section␈α∞on␈α
sequences.
␈↓ ↓H␈↓For example, if ␈↓↓(A, B, C)␈↓ is a sequence, ␈↓s␈↓, then in the new interpretation we should write:
␈↓ ↓H␈↓␈↓ ¬	␈↓s␈↓ = ␈↓{<1, ␈↓↓A␈↓>, <2, ␈↓↓B␈↓>,<3, ␈↓↓C␈↓>}


␈↓ ↓H␈↓Thus␈α∂␈↓s(2)␈↓␈α⊂is␈α∂␈↓↓B␈↓,␈α⊂etc.␈α∂What␈α⊂has␈α∂happened␈α⊂is␈α∂that␈α⊂what␈α∂was␈α⊂previously␈α∂considered␈α⊂to␈α∂be␈α⊂a␈α∂data
␈↓ ↓H␈↓structure␈αhas␈αbecome␈αa␈αfunction,␈αand␈αthe␈αselector␈αfunctions␈αon␈αthe␈αdata␈αstructure␈αhave␈αnow␈αbecome
␈↓ ↓H␈↓static indices on the function.  Or to make things more transparent:
␈↓ ↓H␈↓␈↓ ∧D␈↓s␈↓ = ␈↓{<␈↓αfirst␈↓, ␈↓↓A␈↓>, <␈↓αsecond␈↓, ␈↓↓B␈↓>,<␈↓αthird␈↓, ␈↓↓C␈↓>}

␈↓ ↓H␈↓Then␈α
we␈α
would␈α
write␈α
␈↓s(␈↓αfirst␈↓)␈↓␈α
rather␈α
than␈α
␈↓αfirst␈↓(s)␈↓␈↓π 31␈↓.␈α
 This␈α
idea␈α
can␈α
easily␈α
be␈α
applied␈α∞to␈α
S-exprs
␈↓ ↓H␈↓and␈αtheir␈αfunctions.␈αIn␈αgraphical␈αterms␈αwe␈αare␈αrepresenting␈αthe␈αstructures␈αsuch␈αthat␈αthe␈αarcs␈αof␈αthe
␈↓ ↓H␈↓graph␈αare␈αlabeled␈αwith␈αthe␈αselector␈α
indices.␈αWith␈αL-trees␈αthe␈αlabeling␈αwas␈αimplicit:␈α
left-branch␈αwas
␈↓ ↓H␈↓␈↓αcar␈↓;␈α∩right-branch␈α∩was␈α⊃␈↓αcdr␈↓.␈α∩ With␈α∩explicit␈α⊃labels␈α∩on␈α∩the␈α⊃branches,␈α∩the␈α∩trees␈α∩become␈α⊃unordered.
␈↓ ↓H␈↓Several␈αlanguages␈αimplement␈αsuch␈αunordered␈αtrees;␈αthey␈αare␈αcalled␈α␈↓αstructure␈↓s␈αin␈αAlgol 68␈αand␈αEL1,
␈↓ ↓H␈↓and␈αcalled␈α␈↓αrecord␈↓s␈α
in␈αPascal.␈α Several␈α
formalisms␈αexploit␈αthis␈αview␈α
of␈αdata␈αstructures;␈α
in␈αparticular
␈↓ ↓H␈↓the␈αVienna␈αDefinition␈αLanguage,␈αwhich␈αis␈αa␈αdirect␈αdescendant␈αof␈αLISP,␈αrepresents␈αits␈αdata␈αin␈αsuch
␈↓ ↓H␈↓a manner.

␈↓ ↓H␈↓What␈αthen␈α
is␈αa␈α
data␈αstructure?␈α
It␈αdepends␈α
on␈αhow␈αyou␈α
look␈αat␈α
it.␈αFor␈α
our␈αimmediate␈α
purposes␈αwe
␈↓ ↓H␈↓will␈α
try␈α
to␈α
remain␈α
intuitive␈αand␈α
informal.␈α
 We␈α
will␈α
try␈α
to␈αcharacterize␈α
an␈α
abstract␈α
data␈α
structure␈αas␈α
a
␈↓ ↓H␈↓domain␈αand␈αa␈αcollection␈αof␈αassociated␈α
operations␈αand␈αcontrol␈αstructures.␈αThe␈αoperations␈αand␈α
control
␈↓ ↓H␈↓mechanisms␈α⊃should␈α⊃allow␈α⊃us␈α⊃to␈α⊃describe␈α⊃algorithms␈α⊃in␈α⊃a␈α⊃natural␈α⊃manner␈α⊃but␈α⊃should,␈α⊃if␈α⊃at␈α⊃all
␈↓ ↓H␈↓possible, remain representation independent.

␈↓ ↓H␈↓Now␈αfor␈αsome␈αmore␈αmundane␈αtips␈αand␈αtricks␈αon␈αLISP␈αprogramming.␈α When␈αevaluating␈αor␈αwriting
␈↓ ↓H␈↓functions␈α∞or␈α∞(predicates)␈α∞␈↓↓always␈↓␈α∞keep␈α∂in␈α∞mind␈α∞any␈α∞restrictions␈α∞of␈α∂the␈α∞function:␈α∞is␈α∞it␈α∞partial?␈α∂is␈α∞it
␈↓ ↓H␈↓total?␈α∂defined␈α∂only␈α∂for␈α∂lists?␈α∞are␈α∂there␈α∂restrictions␈α∂on␈α∂arguments?␈α∞When␈α∂taking␈α∂␈↓αcar␈↓␈α∂or␈α∂␈↓αcdr␈↓␈α∂is␈α∞the
␈↓ ↓H␈↓argument non-atomic?

␈↓ ↓H␈↓A␈α
few␈α
tricks␈α
were␈α
embedded␈αin␈α
the␈α
problem␈α
sets.␈α
 Recall␈αproblem␈α
␈↓↓8␈↓␈α
on␈α
page 28.␈α
The␈αcomposition
␈↓ ↓H␈↓␈↓αatom[cons[␈α...]]␈↓␈αwill␈αalways␈αevaluate␈αto␈α␈↓
f␈↓␈α␈↓π 32␈↓␈αsince␈αthe␈αresult␈αof␈α␈↓αcons␈↓-ing␈αis␈αalways␈αnon-atomic.␈α In␈α␈↓↓10.␈↓,
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 31␈↓␈αG.␈αSteele␈αreports␈αthat␈αPPL␈α(Polymorphic␈αProgramming␈αLanguage)␈αat␈αHarvard␈αlets␈αyou␈αdo␈αthis:
␈↓ ↓H␈↓␈↓αcar[s]␈↓ and ␈↓αs[car]␈↓ both work.

␈↓ ↓H␈↓␈↓π 32␈↓␈α
If␈αit␈α
has␈αa␈α
value␈αat␈α
all!␈αIf␈α
the␈α
computation␈αof␈α
the␈αarguments␈α
to␈αthe␈α
␈↓αcons␈↓␈αdoes␈α
not␈α
terminate␈αor
␈↓ ↓H␈↓gives ␈↓λB␈↓ then we obviously won't get ␈↓
f␈↓.
␈↓ ↓H␈↓␈↓↓44  Symbolic expressions␈↓ 42.8␈↓


␈↓ ↓H␈↓we␈αused␈αatoms␈αwith␈αthe␈αsame␈αletter␈αstrings␈αas␈αpredicate␈αnames,␈α␈↓αATOM␈↓␈αand␈α␈↓αEQ␈↓.␈α␈↓αATOM␈↓␈αand␈α␈↓αEQ␈↓␈αare
␈↓ ↓H␈↓perfectly␈αgood␈α
atoms,␈αand␈α
are␈α␈↓↓not␈↓␈α
the␈αLISP␈α
predicates.␈α ␈↓↓14.␈↓␈α
shows␈αthat␈α
predicates␈αare␈αperfectly␈α
good
␈↓ ↓H␈↓expressions␈α
to␈α
evaluate;␈α
e␈↓β1␈↓␈α
is␈α
a␈αpredicate.␈α
Similarly,␈α
␈↓↓16.␈↓␈α
shows␈α
that␈α
some␈α
conditional␈αexpressions␈α
may
␈↓ ↓H␈↓appear within a functional composition.

␈↓ ↓H␈↓Notice␈αthat␈α␈↓αtwist␈↓␈αin␈αII␈αis␈αtotal␈αwhereas␈α␈↓αfindem␈↓␈αis␈αpartial.␈α ␈↓αfindem␈↓␈αis␈αpartial␈αsince␈α␈↓αy␈↓␈αmust␈α
be␈αatomic.
␈↓ ↓H␈↓Both␈αfunctions␈αbuild␈αnew␈αtrees:␈α␈↓αtwistem␈↓␈αreverses␈αleft-␈αand␈αright-branches␈αrecursively;␈α␈↓αfindem␈↓␈αbuilds
␈↓ ↓H␈↓a␈α
tree␈αwith␈α
the␈α
same␈αbranching␈α
structure␈α
as␈α␈↓αx␈↓,␈α
but␈α
the␈αterminal␈α
nodes␈α
contain␈α␈↓αT␈↓␈α
at␈α
the␈αpoints␈α
where
␈↓ ↓H␈↓the atom ␈↓αy␈↓ appears in the original tree, and ␈↓αNIL␈↓ otherwise.

␈↓ ↓H␈↓Now for a representational trick: on page 40 you should have discovered that the value of:

␈↓ ↓H␈↓␈↓ ∧e␈↓αcons[␈↓λα␈↓β1␈↓α; (␈↓λα␈↓β2␈↓α, ...␈↓λα␈↓βn␈↓α)]␈↓ is: ␈↓α(␈↓λα␈↓β1␈↓α, ␈↓λα␈↓β2␈↓α, ... ␈↓λα␈↓βn␈↓α). 

␈↓ ↓H␈↓α␈↓Notice␈αthat␈α␈↓αlist[␈↓λα␈↓β1␈↓α;(␈↓λα␈↓β2␈↓α,␈α...␈α␈↓λα␈↓βn␈↓α)]␈↓␈αis␈α␈↓α(␈↓λα␈↓β1␈↓α␈α(␈↓λα␈↓β2␈↓α␈α...␈α␈↓λα␈↓βn␈↓α))␈↓.␈α So␈α␈↓αcons␈↓␈αwill␈αadd␈αa␈αnew␈αelement␈αto␈αthe␈αfront␈αof␈αan
␈↓ ↓H␈↓existing list. ␈↓αlist␈↓ will create a new list whose elements will be the values of the arguments to ␈↓αlist␈↓.

␈↓ ↓H␈↓Be␈αclear␈αon␈αthe␈αdifference␈αbetween␈αthe␈αrepresentation␈α
of␈αthe␈αempty␈αlist,␈α␈↓αNIL␈↓,␈αand␈αthe␈αlist␈α
consisting
␈↓ ↓H␈↓of␈α∃␈↓αNIL␈↓,␈α∃␈↓α(NIL)␈↓;␈α∃␈↓α(NIL)␈↓␈α∃is␈α∃an␈α∃abbreviation␈α⊗for␈α∃␈↓α(NIL␈α∃.␈α∃NIL)␈↓,␈α∃which␈α∃certainly␈α∃is␈α⊗not␈α∃␈↓αNIL␈↓.
␈↓ ↓H␈↓List-notation is an abbreviation and can always be translated back into a S-expr.

␈↓ ↓H␈↓And␈αan␈αadmonition:␈α
though␈αour␈αrepresentation␈αof␈α
sequences␈αis␈αsuch␈αthat␈α
␈↓αfirst␈↓,␈α␈↓αrest␈↓␈αand␈α
␈↓αconcat␈↓␈αare
␈↓ ↓H␈↓identical␈αto␈α
␈↓αcar␈↓,␈α␈↓αcdr␈↓,␈α
and␈α␈↓αcons␈↓␈α
respectively,␈αwe␈αshould␈α
use␈αthe␈α
names␈α␈↓αfirst␈↓,␈α
␈↓αrest␈↓,␈αand␈α
␈↓αconcat␈↓␈αto␈αmake␈α
it
␈↓ ↓H␈↓clear that we are operating on lists.



␈↓ ↓H␈↓␈↓ ¬,␈↓↓2.9  Becoming an expert␈↓


␈↓ ↓H␈↓We␈α
have␈αalready␈α
traced␈αthe␈α
development␈α
of␈αa␈α
few␈αLISP␈α
algorithms,␈α
and␈αwe␈α
have␈αgiven␈α
a␈αfew␈α
hints
␈↓ ↓H␈↓for␈αthe␈αbudding␈αLISPer.␈αIt␈αis␈αtime␈αto␈αreinforce␈αthese␈αtentative␈αstarts␈αwith␈αan␈αintensive␈αstudy␈αof␈αthe
␈↓ ↓H␈↓techniques␈αfor␈αwriting␈αgood␈αLISP␈αprograms.␈α This␈αsection␈αwill␈αspend␈αa␈αgood␈αdeal␈αof␈αtime␈αshowing
␈↓ ↓H␈↓different␈αstyles␈αof␈αdefinition,␈αgiving␈αhints␈α
about␈αhow␈αto␈αwrite␈αLISP␈αfunctions,␈αand␈α
increasing␈αyour
␈↓ ↓H␈↓familiarity␈αwith␈αLISP.␈α For␈αthose␈αof␈αyou␈αwho␈αare␈αimpatiently␈αwaiting␈αto␈αsee␈αsome␈α␈↓↓real␈↓␈αapplications
␈↓ ↓H␈↓of␈α
this␈α
strange␈α
programming␈α
language,␈α
we␈α
can␈α
only␈α
say␈α
"be␈α
patient".␈α
 The␈α
next␈α
chapter␈α
␈↓↓will␈↓␈α
develop
␈↓ ↓H␈↓several␈αnon-trivial␈αalgorithms,␈αbut␈αwhat␈αwe␈αmust␈αdo␈αfirst␈αis␈αimprove␈αyour␈αskills,␈αeven␈αat␈αthe␈αrisk␈αof
␈↓ ↓H␈↓worsening your disposition.

␈↓ ↓H␈↓First␈α
some␈α∞terminology␈α
is␈α∞appropriate:␈α
the␈α∞style␈α
of␈α∞definition␈α
which␈α∞we␈α
have␈α∞been␈α
using␈α∞is␈α
called
␈↓ ↓H␈↓␈↓↓definition by recursion␈↓. The basic components of such a definition are:
␈↓ ↓H␈↓␈↓↓2.9␈↓ λxBecoming an expert     45␈↓



␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓␈αA␈αbasis␈αcase:␈αwhat␈αto␈αcompute␈αas␈αvalue␈αfor␈αthe␈αfunction␈αin␈αone␈αor␈αmore␈αparticularly
␈↓ ↓H␈↓␈↓ αhsimple cases. A basis case is frequently referred to as a termination case.

␈↓ ↓H␈↓␈↓	REC␈↓

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓␈αA␈αgeneral␈αcase:␈αwhat␈αto␈αcompute␈αas␈αvalue␈αfor␈αa␈αfunction,␈αgiven␈αthe␈αvalues␈αof␈αone␈αor
␈↓ ↓H␈↓␈↓ αhmore previous computations on that function.

␈↓ ↓H␈↓You␈α_should␈α_compare␈α_the␈α_structure␈α_of␈α_a␈α_␈↓	REC␈↓-definition␈α_of␈α_a␈α_function␈α_with␈α_that␈α→of␈α_an
␈↓ ↓H␈↓␈↓	IND␈↓-definition␈α∞of␈α∞a␈α∞set␈α∞(see␈α∞␈↓	IND␈↓␈α∞on␈α∞page 8).␈α∞ Applications␈α∞of␈α∞␈↓	REC␈↓-definitions␈α∂are␈α∞particularly
␈↓ ↓H␈↓useful␈α∩in␈α∩computing␈α∩values␈α∩of␈α⊃a␈α∩function␈α∩defined␈α∩over␈α∩a␈α⊃set␈α∩which␈α∩has␈α∩been␈α∩defined␈α∩by␈α⊃an
␈↓ ↓H␈↓␈↓	IND␈↓-definition.␈αFor␈αassume␈α
that␈αwe␈αhave␈αdefined␈α
a␈αset␈α␈↓A␈↓␈αusing␈α
␈↓	IND␈↓␈αthen␈αa␈αplausible␈α
recipe␈αfor
␈↓ ↓H␈↓computing␈α
a␈α
function␈α
␈↓f␈↓␈α∞over␈α
␈↓A␈↓␈α
would␈α
involve␈α∞two␈α
parts:␈α
first,␈α
tell␈α
how␈α∞to␈α
compute␈α
␈↓f␈↓␈α
on␈α∞the␈α
base
␈↓ ↓H␈↓domain␈αof␈α␈↓A␈↓,␈αand␈αsecond,␈αgiven␈αvalues␈αfor␈αsome␈α
elements␈αof␈α␈↓A␈↓␈αsay␈α␈↓a␈↓β1␈↓, ...,␈↓a␈↓βn␈↓,␈αuse␈α␈↓	IND␈↓␈αto␈αgenerate␈α
a
␈↓ ↓H␈↓new␈αelement␈α␈↓a␈↓;␈αthen␈αspecify␈αthe␈αvalue␈αof␈α␈↓f(a)␈↓␈αas␈αa␈αfunction␈αof␈αthe␈αknown␈αvalues␈αof␈α␈↓f(a␈↓β1␈↓)␈↓, ..., ␈α␈↓f(a␈↓βn␈↓)␈↓.
␈↓ ↓H␈↓That is exactly the structure of ␈↓	REC␈↓.

␈↓ ↓H␈↓Here␈α
is␈αanother␈α
attribute␈α
of␈α␈↓	IND␈↓-definitions:␈α
If␈α
we␈αhave␈α
defined␈αa␈α
set␈α
␈↓A␈↓␈αusing␈α
␈↓	IND␈↓,␈α
assume␈αwe
␈↓ ↓H␈↓wish to prove that a certain property ␈↓	P␈↓ holds for every element of ␈↓A␈↓. We need only show that:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓ ␈↓	P␈↓ holds for every element of the base domain of ␈↓A␈↓.

␈↓ ↓H␈↓␈↓	PRF␈↓

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓␈αUsing␈αthe␈αtechnique␈αwe␈αelaborated␈αin␈αdefining␈αthe␈αfunction␈α␈↓f␈↓␈αabove,␈αif␈αwe␈αcan␈αshow
␈↓ ↓H␈↓␈↓ αhthat␈α
␈↓	P␈↓␈α
holds␈α
for␈α
the␈α
new␈α
element␈α
perhaps␈α
relying␈α
on␈α
proofs␈α
of␈α
␈↓	P␈↓␈α
for␈αsub-elements,␈α
then
␈↓ ↓H␈↓␈↓ αhwe should have a convincing argument that ␈↓	P␈↓ holds over ␈↓↓all␈↓ of ␈↓A␈↓.

␈↓ ↓H␈↓This␈α⊂proof␈α∂technique␈α⊂is␈α∂a␈α⊂generalization␈α⊂of␈α∂a␈α⊂common␈α∂technique␈α⊂for␈α∂proving␈α⊂properties␈α⊂of␈α∂the
␈↓ ↓H␈↓integers. In that context it is called mathematical induction.

␈↓ ↓H␈↓So␈αwe␈α
are␈αseeing␈αan␈α
interesting␈αparallel␈α
between␈αinductive␈αdefinitions␈α
of␈αsets,␈α
recursive␈αdefinitions
␈↓ ↓H␈↓of␈α⊃functions,␈α⊃and␈α⊃proofs␈α⊃by␈α⊃induction.␈α⊃ As␈α⊃we␈α⊃proceed␈α⊃we␈α⊃will␈α⊃exploit␈α⊃various␈α⊃aspects␈α⊃of␈α⊂this
␈↓ ↓H␈↓interrelationship.␈α⊂ However␈α⊂our␈α⊂task␈α⊂at␈α⊂hand␈α⊂is␈α⊂more␈α⊂mundane:␈α⊂to␈α⊂develop␈α⊂facility␈α⊂at␈α⊂applying
␈↓ ↓H␈↓␈↓	REC␈↓␈α∂to␈α⊂define␈α∂functions␈α∂over␈α⊂the␈α∂␈↓	IND␈↓-domains␈α⊂of␈α∂symbolic␈α∂expressions,␈α⊂␈↓	S␈↓,␈α∂and␈α⊂of␈α∂sequences,
␈↓ ↓H␈↓␈↓	Seq␈↓.

␈↓ ↓H␈↓First␈α
let's␈α
be␈αreassured␈α
that␈α
the␈α
functions␈αwe␈α
have␈α
constructed␈α
so␈αfar␈α
do␈α
indeed␈α
satisfy␈α␈↓	REC␈↓.␈α
 Recall
␈↓ ↓H␈↓our␈α
example␈α∞of␈α
␈↓αequal␈↓␈α∞on␈α
page 27.␈α∞The␈α
basis␈α∞case␈α
involves␈α∞a␈α
calculation␈α∞on␈α
members␈α∞of␈α
␈↓<atom>␈↓;
␈↓ ↓H␈↓there␈α∩we␈α∩rely␈α∩on␈α∩␈↓αeq␈↓␈α∩to␈α∩distinguish␈α⊃between␈α∩distinct␈α∩atoms.␈α∩ The␈α∩question␈α∩of␈α∩equality␈α∩for␈α⊃two
␈↓ ↓H␈↓non-atomic␈α
S-exprs␈α
was␈α
thrown␈α
back␈α
to␈α
the␈α
question␈α
of␈α
equality␈α
for␈α
their␈α
␈↓αcar␈↓s␈α
and␈α
␈↓αcdr␈↓s.␈α
But␈αthat
␈↓ ↓H␈↓too,␈α
is␈α
the␈α
right␈α
thing␈α
to␈α
do␈α
since␈α
the␈α
constructed␈α
object␈α
is␈α
in␈α
fact␈α
manufactured␈α
by␈α
␈↓αcons␈↓,␈α
and␈α␈↓αcar␈↓
␈↓ ↓H␈↓and ␈↓αcdr␈↓ of that object get the components.
␈↓ ↓H␈↓␈↓↓46  Symbolic expressions␈↓ 52.9␈↓


␈↓ ↓H␈↓Similar␈α⊂justification␈α∂for␈α⊂␈↓αlength␈↓␈α∂on␈α⊂page 33␈α∂can␈α⊂be␈α∂given.␈α⊂ Here␈α∂the␈α⊂domain␈α∂is␈α⊂␈↓	Seq␈↓.␈α⊂The␈α∂base
␈↓ ↓H␈↓domain␈αis␈αthe␈αempty␈α
sequence,␈αand␈α␈↓αlength␈↓␈αis␈αdefined␈α
to␈αgive␈α␈↓α0␈↓␈αin␈αthat␈α
case.␈αThe␈αgeneral␈αcase␈αin␈α
the
␈↓ ↓H␈↓recursion␈αcomes␈αfrom␈αthe␈α␈↓	IND␈↓-definition␈αof␈αa␈αsequence␈↓π 33␈↓.␈α There,␈αgiven␈αa␈αsequence␈α␈↓↓s␈↓,␈αwe␈αmade␈αa
␈↓ ↓H␈↓new␈αsequence␈αby␈αadding␈αa␈αsequence␈αelement␈αto␈αthe␈αthe␈αfront␈αof␈α␈↓↓s␈↓.␈αAgain␈αthe␈αcomputation␈αof␈α␈↓αlength␈↓
␈↓ ↓H␈↓parallels␈αthis␈αconstruction,␈αsaying␈αthat␈αthe␈αlength␈αof␈αthis␈αnew␈αsequence␈αis␈αone␈αmore␈αthan␈αthe␈αlength
␈↓ ↓H␈↓of ␈↓↓s␈↓.

␈↓ ↓H␈↓For a more traditional example consider the factorial function, n!.

␈↓ ↓H␈↓␈↓↓1.␈↓ The function is defined for non-negative integers.

␈↓ ↓H␈↓␈↓↓2.␈↓ The value of the function for 0 is 1.

␈↓ ↓H␈↓␈↓↓3␈↓.  Otherwise the value of n! is n times the value of (n-1)!.

␈↓ ↓H␈↓It should now be clear how to write a LISP program for the factorial function:
␈↓ ↓H␈↓α␈↓ β|fact[n] <= [eq[n;0] → 1; ␈↓
t␈↓α → times[n;fact[sub1[n]]]] ␈↓π 34␈↓α


␈↓ ↓H␈↓The␈αimplication␈α
here␈αis␈α
that␈αit␈α
is␈αsomehow␈αeasier␈α
to␈αcompute␈α
(n-1)!␈αthan␈α
to␈αcompute␈α
n!.␈αBut␈αthat␈α
too
␈↓ ↓H␈↓is in accord with our construction of the integers using the successor function.

␈↓ ↓H␈↓These␈α∩examples␈α∩are␈α∩typical␈α∩of␈α∩LISP's␈α∩recursive␈α∩definitions.␈α∩ The␈α∩body␈α∩of␈α∩the␈α∩definition␈α∩is␈α∩a
␈↓ ↓H␈↓conditional␈αexpression;␈αthe␈αfirst␈αfew␈αbranches␈αinvolve␈αspecial␈αcases,␈αcalled␈α
␈↓↓termination␈αconditions␈↓.
␈↓ ↓H␈↓Then␈αthe␈αremainder␈αof␈αthe␈αconditional␈αcovers␈αthe␈αgeneral␈αcase--␈αwhat␈αto␈αdo␈αif␈αthe␈αargument␈αto␈αthe
␈↓ ↓H␈↓function is not one of the special cases.

␈↓ ↓H␈↓Notice␈α⊂that␈α⊂␈↓αfact␈↓␈α⊂is␈α⊂a␈α⊂partial␈α⊂function,␈α⊂defined␈α⊂only␈α⊂for␈α⊂non-negative␈α⊂integers.␈α⊂When␈α⊂writing␈α⊂or
␈↓ ↓H␈↓reading␈α∂LISP␈α∂definitions␈α∂pay␈α∂particular␈α∂attention␈α∂to␈α∞the␈α∂domain␈α∂of␈α∂definition␈α∂and␈α∂the␈α∂range␈α∞of
␈↓ ↓H␈↓values produced. The following general hints should also be useful:

␈↓ ↓H␈↓␈↓↓1␈↓.␈α∪ Is␈α∩it␈α∪a␈α∩function␈α∪or␈α∩predicate?␈α∪This␈α∩information␈α∪can␈α∩be␈α∪used␈α∩to␈α∪double-check␈α∩uses␈α∪of␈α∩the
␈↓ ↓H␈↓definition.␈α⊂Don't␈α∂use␈α⊂a␈α∂predicate␈α⊂where␈α⊂a␈α∂S-expr-valued␈α⊂function␈α∂is␈α⊂expected;␈α∂and␈α⊂don't␈α⊂use␈α∂an
␈↓ ↓H␈↓S-expr-valued function where a list-value is expected.

␈↓ ↓H␈↓␈↓↓2␈↓.␈α
 Are␈αthere␈α
any␈αrestrictions␈α
on␈αthe␈α
argument␈αpositions?␈α
Similar␈αconsistency␈α
checking␈αas␈α
in␈α
␈↓↓1.␈↓␈αcan
␈↓ ↓H␈↓be done with this information.

␈↓ ↓H␈↓␈↓↓3␈↓.␈α∂ Are␈α∂the␈α∞termination␈α∂conditions␈α∂compatible␈α∂with␈α∞the␈α∂restrictions␈α∂on␈α∂the␈α∞arguments?␈α∂ If␈α∂it␈α∂is␈α∞a
␈↓ ↓H␈↓recursion␈αon␈α
lists,␈αcheck␈αfor␈α
the␈αempty␈α
list;␈αif␈αit␈α
is␈αa␈α
recursion␈αof␈αarbitrary␈α
S-exprs,␈αthen␈α
check␈αfor
␈↓ ↓H␈↓the appearance of an atom.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 33␈↓␈α∩Note␈α∩(page 30)␈α∩that␈α∪we␈α∩didn't␈α∩give␈α∩an␈α∩explicit␈α∪␈↓	IND␈↓-definition,␈α∩but␈α∩rather␈α∩a␈α∩set␈α∪of␈α∩BNF
␈↓ ↓H␈↓equations. The reader can easily supply the explict definition.

␈↓ ↓H␈↓α␈↓π 34␈↓α times␈↓ is assumed to be a LISP function which performs multiplication, and ␈↓αsub1[n] <= n-1␈↓.
␈↓ ↓H␈↓␈↓↓2.9␈↓ λxBecoming an expert     47␈↓


␈↓ ↓H␈↓␈↓↓4␈↓.␈α Whenever␈αa␈αfunction␈αcall␈αis␈αmade␈αwithin␈αthe␈αdefinition,␈αare␈αall␈αthe␈αrestrictions␈αon␈αthat␈αfunction
␈↓ ↓H␈↓satisfied?

␈↓ ↓H␈↓␈↓↓5␈↓.␈α⊂ Don't␈α⊂try␈α⊂to␈α⊂do␈α⊂too␈α⊂much.␈α⊂Be␈α⊂lazy.␈α⊂There␈α⊂is␈α⊂usually␈α⊂a␈α⊂very␈α⊂simple␈α⊂termination␈α⊂case.␈α⊂If␈α∂the
␈↓ ↓H␈↓termination␈α⊃case␈α⊃looks␈α⊃messy,␈α∩there␈α⊃is␈α⊃probably␈α⊃something␈α∩wrong␈α⊃with␈α⊃your␈α⊃conception␈α∩of␈α⊃the
␈↓ ↓H␈↓program.␈α
 If␈αthe␈α
general␈αcase␈α
looks␈αmessy,␈α
then␈α
write␈αsome␈α
subfunctions␈αto␈α
perform␈αthe␈α
brunt␈αof␈α
the
␈↓ ↓H␈↓calculation.

␈↓ ↓H␈↓Use␈αthe␈αabove␈αsuggestions␈αwhen␈αwriting␈αany␈αsubfunction.␈αWhen␈αyou␈αare␈αfinished,␈αno␈αfunction␈αwill
␈↓ ↓H␈↓do␈αvery␈α
much,␈αbut␈αthe␈α
net␈αeffect␈αof␈α
all␈αthe␈α
functions␈αacting␈αin␈α
concert␈αis␈αa␈α
solution␈αto␈αyour␈α
problem.
␈↓ ↓H␈↓That is part of the mystery of recursive programming.

␈↓ ↓H␈↓As␈αyou␈αmost␈αlikely␈αhave␈αdiscovered,␈αthe␈αreal␈αsticky␈αbusiness␈αin␈αLISP␈αprogramming␈αis␈αwriting␈αyour
␈↓ ↓H␈↓own␈α∞programs.␈α∂But␈α∞who␈α∂says␈α∞programming␈α∞is␈α∂easy?␈α∞LISP␈α∂at␈α∞least␈α∞makes␈α∂some␈α∞of␈α∂your␈α∞decisions
␈↓ ↓H␈↓easy.␈α⊃Its␈α∩structure␈α⊃is␈α∩particularly␈α⊃spartan.␈α∩So␈α⊃far␈α⊃there␈α∩is␈α⊃only␈α∩␈↓↓one␈↓␈α⊃way␈α∩to␈α⊃write␈α∩a␈α⊃non-trivial
␈↓ ↓H␈↓algorithm␈α⊂in␈α⊂LISP:␈α∂use␈α⊂recursion.␈α⊂The␈α∂structure␈α⊂of␈α⊂the␈α∂program␈α⊂goes␈α⊂like␈α∂that␈α⊂of␈α⊂an␈α∂inductive
␈↓ ↓H␈↓argument.␈α⊃Find␈α⊂the␈α⊃right␈α⊂induction␈α⊃hypothesis␈α⊃and␈α⊂the␈α⊃inductive␈α⊂proof␈α⊃is␈α⊂easy;␈α⊃find␈α⊃the␈α⊂right
␈↓ ↓H␈↓structure␈αto␈αrecur␈αon␈αand␈αrecursive␈αprogramming␈α
is␈αeasy.␈αIt's␈αeasier␈αto␈αbegin␈αwith␈α
unary␈αfunctions;
␈↓ ↓H␈↓then␈α∞there's␈α
no␈α∞question␈α
about␈α∞what␈α
to␈α∞recur␈α
on.␈α∞The␈α
only␈α∞decision␈α
now␈α∞is␈α
how␈α∞to␈α∞terminate␈α
the
␈↓ ↓H␈↓recursion.␈α If␈αthe␈αargument␈αis␈α
an␈αS-expr␈αwe␈αtypically␈αterminate␈αon␈α
the␈αoccurrence␈αof␈αan␈αatom.␈αIf␈α
the
␈↓ ↓H␈↓argument is a list then terminate on ␈↓α()␈↓. If the argument is a number then terminate on zero.

␈↓ ↓H␈↓First␈αlet's␈αconsider␈α
a␈αslightly␈αmore␈αcomplicated␈α
arithmetical␈αexample,␈αthe␈α
Fibonacci␈αsequence:␈α0,␈α1,␈α
1,
␈↓ ↓H␈↓2, 3, 5, 8, ... . This sequence is frequently characterized by the following recurrence relation:
␈↓ ↓H␈↓␈↓ ε f(0) = 0
␈↓ ↓H␈↓␈↓ ε f(1) = 1
␈↓ ↓H␈↓␈↓ ¬Wf(n) = f(n-1)+f(n-2);

␈↓ ↓H␈↓The translation to a LISP function is easy:
␈↓ ↓H␈↓α␈↓ β8fib[n] <=␈↓ ∧X[eq[n;0] → 0;
␈↓ ↓H␈↓α␈↓ β8␈↓ ∧X eq[n;1] → 1;
␈↓ ↓H␈↓α␈↓ β8␈↓ ∧X ␈↓
t␈↓α → plus[fib[sub1[n]];fib[sub1[sub1[n]]]]]

␈↓ ↓H␈↓where ␈↓αplus␈↓ is a representation of the mathematical function, ␈↓α+␈↓.

␈↓ ↓H␈↓A␈α∞few␈α
points␈α∞can␈α∞be␈α
made␈α∞here.␈α
First,␈α∞notice␈α∞that␈α
the␈α∞intuitive␈α
evaluation␈α∞scheme␈α∞requires␈α
many
␈↓ ↓H␈↓duplications␈αof␈αcomputation.␈α For␈α
example,␈αcomputation␈αof␈α␈↓αfib[5]␈↓␈α
requires␈αthe␈αcomputation␈αof␈α
␈↓αfib[4]␈↓
␈↓ ↓H␈↓and␈α
␈↓αfib[3]␈↓.␈α But␈α
within␈αthe␈α
calculation␈α
of␈α␈↓αfib[4]␈↓␈α
we␈αmust␈α
again␈α
calculate␈α␈↓αfib[3]␈↓,␈α
etc.␈α It␈α
would␈αbe␈α
nice
␈↓ ↓H␈↓if␈α∂we␈α∂could␈α∂restructure␈α∂the␈α∂definition␈α∂of␈α⊂the␈α∂function␈α∂␈↓αfib␈↓␈α∂to␈α∂stop␈α∂this␈α∂duplication␈α⊂of␈α∂calculation.
␈↓ ↓H␈↓Since␈αwe␈α␈↓↓do␈↓␈α
wish␈αto␈αrun␈α
programs␈αon␈αa␈αmachine␈α
we␈αshould␈αgive␈α
some␈αattention␈αto␈α
efficiency.␈α To
␈↓ ↓H␈↓those␈α∀with␈α∀programming␈α∀experience,␈α∀the␈α∀solution␈α∀is␈α∀easy:␈α∀assign␈α∀the␈α∀partial␈α∀computations␈α∪to
␈↓ ↓H␈↓temporary␈α∀variables.␈α∪ The␈α∀problem␈α∀here␈α∪is␈α∀that␈α∪our␈α∀current␈α∀subset␈α∪of␈α∀LISP␈α∀doesn't␈α∪contain
␈↓ ↓H␈↓assignment. There is, however, a very useful technique which we can apply.

␈↓ ↓H␈↓We␈αwill␈αdefine␈αanother␈αfunction,␈αcalled␈α␈↓αfib␈↓λ'␈↓,␈αon␈αthree␈αvariables␈α␈↓αx␈↓,␈α␈↓αy␈↓,␈αand␈α␈↓αn␈↓.␈αThe␈αvariables,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,
␈↓ ↓H␈↓will be used to carry the partial computations. Consider:
␈↓ ↓H␈↓␈↓↓48  Symbolic expressions␈↓ 52.9␈↓


␈↓ ↓H␈↓α␈↓ ¬Lfib␈↓β1␈↓α[n] <= fib␈↓λ'␈↓α[n;0;1]

␈↓ ↓H␈↓α␈↓ βHfib␈↓λ'␈↓α[n;x;y] <=␈↓ ¬X[eq[n;0] → x;
␈↓ ↓H␈↓α␈↓ βH␈↓ ¬X ␈↓
t␈↓α → fib␈↓λ'␈↓α[sub1[n];plus[x;y];x]]

␈↓ ↓H␈↓This␈αexample␈αis␈α
complicated␈αenough␈αto␈αwarrant␈α
examination.␈αThe␈αinitial␈α
call,␈α␈↓αfib␈↓β1␈↓α[n]␈↓,␈αhas␈αthe␈α
effect
␈↓ ↓H␈↓of␈α
calling␈α
␈↓αfib␈↓λ'␈↓␈αwith␈α
␈↓αx␈↓␈α
initialized␈αto␈α
␈↓α0␈↓␈α
and␈α
with␈α␈↓αy␈↓␈α
initialized␈α
to␈α␈↓α1␈↓.␈α
The␈α
calls␈α
on␈α␈↓αfib␈↓λ'␈↓␈α
within␈α
the␈αbody␈α
of
␈↓ ↓H␈↓the␈αdefinition,␈αsay␈αthe␈αi␈↓πth␈↓␈αsuch␈αrecursive␈αcall,␈αhas␈αthe␈αeffect␈αof␈αsaving␈αthe␈αi␈↓πth␈↓␈αFibonacci␈αnumber␈αin␈α
␈↓αx␈↓
␈↓ ↓H␈↓and the i-1␈↓πst␈↓ in ␈↓αy␈↓.  For example:
␈↓ ↓H␈↓α␈↓ ∧Hfib␈↓β1␈↓α[4]␈↓ ελ= fib␈↓λ'␈↓α[4;0;1]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[3;1;0]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[2;1;1]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[1;2;1]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= fib␈↓λ'␈↓α[0;3;2]
␈↓ ↓H␈↓α␈↓ ∧H␈↓ ελ= 3

␈↓ ↓H␈↓This␈αsame␈αtrick␈αof␈αusing␈αauxiliary␈αfunctions␈α
can␈αbe␈αapplied␈αto␈αthe␈αfactorial␈αexample.␈αWhen␈α
viewed
␈↓ ↓H␈↓computationally,␈αthe␈αresulting␈αdefinition␈αwill␈αbe␈αmore␈αefficient,␈αthough␈αthe␈αgain␈αin␈αefficiency␈αis␈αnot
␈↓ ↓H␈↓as apparent as that in the Fibonacci example ␈↓π 35␈↓. Thus:
␈↓ ↓H␈↓α␈↓ ¬Efact␈↓β1␈↓α[n] <= fact␈↓λ'␈↓α[n;1];

␈↓ ↓H␈↓α␈↓ βnfact␈↓λ'␈↓α[n;x] <= [eq[n;0] → x; ␈↓
t␈↓α → fact␈↓λ'␈↓α[sub1[n];times[n;x]]]

␈↓ ↓H␈↓It␈αis␈αclear␈α
in␈αthese␈αexamples␈α
that␈αthe␈αfunctions␈α␈↓αfact,␈α
fact␈↓β1␈↓␈αand␈α␈↓αfib,␈α
fib␈↓β1␈↓␈αare␈αequivalent.␈α
Perhaps␈αwe
␈↓ ↓H␈↓should␈α∞prove␈α∂that␈α∞this␈α∂is␈α∞so.␈α∞ We␈α∂presented␈α∞the␈α∂crucial␈α∞ideas␈α∞for␈α∂the␈α∞proof␈α∂in␈α∞the␈α∂discussion␈α∞on
␈↓ ↓H␈↓page 45␈α⊗concerning␈α⊗␈↓	IND␈↓,␈α⊗␈↓	REC␈↓␈α⊗and␈α⊗␈↓	PRF␈↓.␈α⊗ We␈α⊗shall␈α⊗examine␈α⊗the␈α⊗question␈α⊗of␈α⊗proofs␈α⊗of
␈↓ ↓H␈↓equivalence in Section 3.9.

␈↓ ↓H␈↓The trick of auxiliary functions is clearly applicable to LISP functions defined over S-exprs:
␈↓ ↓H␈↓α␈↓ βnlength[n] <= [null[n] → 0; ␈↓
t␈↓α → add1[length[rest[n]]]] ␈↓π 36␈↓α

␈↓ ↓H␈↓α␈↓ ¬(length␈↓β1␈↓α[n] <= length␈↓λ'␈↓α[n;0]

␈↓ ↓H␈↓α␈↓ βblength␈↓λ'␈↓α[n;x] <= [null[n] → x; ␈↓
t␈↓α → length␈↓λ'␈↓α[rest[n];add1[x]]]
␈↓ ↓H␈↓α␈↓and it seems apparent that ␈↓αlength[n]␈↓ is equivalent to ␈↓αlength␈↓β1␈↓α[n]␈↓.

␈↓ ↓H␈↓So␈αfar␈αour␈αexamples␈αhave␈αeither␈αbeen␈αnumerical␈αor␈αhave␈αbeen␈αpredicates.␈α Predicates␈αonly␈αrequire
␈↓ ↓H␈↓traversing␈α∪existing␈α∪S-exprs;␈α∪certainly␈α∩we␈α∪will␈α∪want␈α∪to␈α∩write␈α∪algorithms␈α∪to␈α∪build␈α∪new␈α∩S-exprs.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 35␈↓␈αThe␈α␈↓αfib␈↓β1␈↓␈αexample␈α
improves␈αefficiency␈αmostly␈αby␈α
calculating␈αfewer␈αintermediate␈αresults.␈αThe␈α
gain
␈↓ ↓H␈↓in␈αthe␈α␈↓αfact␈↓β1␈↓␈αexample␈αis␈αinvolved␈αwith␈αthe␈αmachinery␈αnecessary␈αto␈αactually␈αexecute␈αthe␈αprogram:␈αthe
␈↓ ↓H␈↓run-time␈α∂environment,␈α∂if␈α∂you␈α∂wish.␈α∂We␈α⊂will␈α∂discuss␈α∂this␈α∂when␈α∂we␈α∂talk␈α∂about␈α⊂implementation␈α∂of
␈↓ ↓H␈↓LISP in Chapter . The whole question of: "what is efficient?" is open to discussion.

␈↓ ↓H␈↓α␈↓π 36␈↓α add1[x] <= x+1
␈↓ ↓H␈↓␈↓↓2.9␈↓ λxBecoming an expert     49␈↓


␈↓ ↓H␈↓Consider␈αthe␈αproblem␈αof␈αwriting␈αa␈αLISP␈αalgorithm␈αto␈αreverse␈αa␈αlist␈α␈↓αx␈↓.␈αThe␈αintuitive␈αcomputation␈αis
␈↓ ↓H␈↓quite␈αsimple.␈αTake␈αelements␈αoff␈αof␈α␈↓αx␈↓␈αone␈αat␈αa␈αtime␈αand␈αput␈αthem␈αonto␈αa␈αnew␈αlist␈α␈↓αy␈↓;␈αas␈αinitialization,
␈↓ ↓H␈↓␈↓αy␈↓ should be ␈↓α()␈↓ and the process should terminate when ␈↓αx␈↓ is empty. Thus:

␈↓ ↓H␈↓α␈↓ ¬8x␈↓ πHy
␈↓ ↓H␈↓α␈↓ ¬8(A B C D)␈↓ πH( )
␈↓ ↓H␈↓α␈↓ ¬8(B C D)␈↓ πH(A)
␈↓ ↓H␈↓α␈↓ ¬8(C D)␈↓ πH(B A)
␈↓ ↓H␈↓α␈↓ ¬8(D)␈↓ πH(C B A)
␈↓ ↓H␈↓α␈↓ ¬8( )␈↓ πH(D C B A)

␈↓ ↓H␈↓Here's␈α
a␈α
plausible␈α␈↓αreverse␈↓;␈α
notice␈α
that␈α
we␈αuse␈α
a␈α
sub-function␈α␈↓αrev␈↓λ'␈↓␈α
to␈α
do␈α
the␈αhard␈α
work␈α
and␈αsneak␈α
the
␈↓ ↓H␈↓initialization in on the second argument to ␈↓αrev␈↓λ'␈↓.

␈↓ ↓H␈↓α␈↓ ¬:reverse[x] <= rev␈↓λ'␈↓α[x;( )]

␈↓ ↓H␈↓α␈↓ βWrev␈↓λ'␈↓α[x;y] <= [null[x] → y; ␈↓
t␈↓α → rev␈↓λ'␈↓α[rest[x];concat[first[x];y]]]

␈↓ ↓H␈↓The␈αfunction␈α␈↓αreverse␈↓␈αbuilds␈αa␈αlist␈αwhich␈αis␈αthe␈αreverse␈αof␈αits␈αargument.␈α Notice␈αthat␈αthis␈αdefinition
␈↓ ↓H␈↓uses␈α
an␈α
auxiliary␈α
function.␈α
 Sometimes␈α
it␈α
is␈α
more␈α
natural␈α
to␈α
express␈α
algorithms␈α
this␈α
way.␈α∞We␈α
will
␈↓ ↓H␈↓see a "direct" definition of the reversing function in a moment.

␈↓ ↓H␈↓This␈α∂␈↓αreverse␈↓␈α⊂function␈α∂builds␈α⊂up␈α∂the␈α⊂new␈α∂list␈α∂in␈α⊂a␈α∂very␈α⊂straightforward␈α∂mannner,␈α⊂␈↓αconcat␈↓-ing␈α∂the
␈↓ ↓H␈↓elements␈α∞onto␈α
the␈α∞second␈α
argument␈α∞of␈α∞␈↓αrev␈↓λ'␈↓α␈↓.␈α
Since␈α∞␈↓αy␈↓␈α
was␈α∞initialized␈α∞to␈α
␈↓α( )␈↓␈α∞we␈α
are␈α∞assured␈α∞that␈α
the
␈↓ ↓H␈↓resulting construct will be a list.

␈↓ ↓H␈↓Construction␈α
is␈α∞usually␈α
not␈α∞quite␈α
so␈α∞straightforward.␈α
Suppose␈α
we␈α∞wish␈α
to␈α∞define␈α
a␈α∞LISP␈α
function
␈↓ ↓H␈↓named␈α␈↓αappend␈↓␈αof␈αtwo␈αlist␈αarguments,␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓,␈αwhich␈αis␈αto␈αreturn␈αa␈αnew␈αlist␈αwhich␈αhas␈α␈↓αx␈↓␈αappended
␈↓ ↓H␈↓onto the front of ␈↓αy␈↓.  For example:

␈↓ ↓H␈↓α␈↓ ∧Sappend[(A B D);(C E)] = (A B D C E)
␈↓ ↓H␈↓α␈↓ ∧(append[A;(B C)] ␈↓ is undefined␈↓α. A␈↓ is not a list.
␈↓ ↓H␈↓␈↓ βZ␈↓αappend[(A B C);NIL] = append[NIL;(A B C)] = (A B C)

␈↓ ↓H␈↓So␈α∩␈↓αappend␈↓␈α∪is␈α∩a␈α∪partial␈α∩function.␈α∩It␈α∪should␈α∩be␈α∪defined␈α∩by␈α∩recursion,␈α∪but␈α∩recursion␈α∪on␈α∩which
␈↓ ↓H␈↓argument?␈α Well,␈α
if␈αeither␈α
argument␈αis␈α
␈↓α( )␈↓␈αthen␈αthe␈α
value␈αgiven␈α
by␈α␈↓αappend␈↓␈α
is␈αthe␈α
other␈αargument.
␈↓ ↓H␈↓The␈α
next␈α
simplest␈α
case␈α
is␈α
a␈α
one-element␈α
list;␈α
if␈α
exactly␈α
one␈α
of␈α
␈↓αx␈↓␈α
or␈α
␈↓αy␈↓␈α
is␈α
a␈α
singleton␈α
how␈α
does␈α
that
␈↓ ↓H␈↓help␈αus␈αdiscover␈αthe␈α
recurrence␈αrelation␈αfor␈αappending?␈α
It␈αdoesn't␈αhelp␈αmuch␈α
if␈α␈↓αy␈↓␈αis␈αa␈αsingleton;␈α
but
␈↓ ↓H␈↓if ␈↓αx␈↓ is, then ␈↓αappend␈↓ could give:

␈↓ ↓H␈↓␈↓ ¬+␈↓αconcat[first[x];y]␈↓ as result. 

␈↓ ↓H␈↓So recursion on ␈↓αx␈↓ is likely. The definition follows easily now.

␈↓ ↓H␈↓␈↓ β%␈↓αappend[x;y] <= [null[x] → y; ␈↓
t␈↓α → concat[first[x];append[rest[x];y]]].␈↓ 
␈↓ ↓H␈↓␈↓↓50  Symbolic expressions␈↓ 52.9␈↓


␈↓ ↓H␈↓Notice␈αthat␈αthe␈αconstruction␈αof␈αthe␈αresult␈αis␈α
a␈αbit␈αmore␈αobscure␈αthan␈αthat␈αinvolved␈αin␈α
␈↓αreverse␈↓.␈αThe
␈↓ ↓H␈↓construction has to "wait" until we have seen the end of the list ␈↓αx␈↓. For example:
␈↓ ↓H␈↓α␈↓ αXappend[(A B C);(D E F)]␈↓ ε8= concat[A;append[(B C);(D E F)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;append[(C);(D E F)]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;concat[C;append[( );(D E F)]]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;concat[C;(D E F)]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;concat[B;(C D E F)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= concat[A;(B C D E F)]
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8= (A B C D E F)

␈↓ ↓H␈↓We␈α
are␈αassured␈α
of␈αconstructing␈α
a␈αlist␈α
here␈αbecause␈α
␈↓αy␈↓␈α
is␈αa␈α
list␈αand␈α
we␈αare␈α
␈↓αconcat␈↓-ing␈αonto␈α
the␈αfront␈α
of
␈↓ ↓H␈↓it.␈αLISP␈αfunctions␈αwhich␈αare␈αto␈αconstruct␈αlist␈αoutput␈αby␈α␈↓αconcat␈↓-ing␈α␈↓↓must␈↓␈α␈↓αconcat␈↓␈αonto␈αthe␈αfront␈αof␈αan
␈↓ ↓H␈↓existing␈α␈↓↓list␈↓.␈αThat␈αlist␈αmay␈αbe␈αeither␈αnon-empty␈α
or␈αthe␈αempty␈αlist,␈α␈↓α( )␈↓.␈αThis␈αis␈αwhy␈α
the␈αtermination
␈↓ ↓H␈↓condition on a list-constructing function, such as the following function, ␈↓αdotem␈↓, returns ␈↓α( )␈↓.

␈↓ ↓H␈↓The␈αarguments␈αto␈α␈↓αdotem␈↓␈αare␈α
both␈αlists␈αassumed␈αto␈αcontain␈α
the␈αsame␈αnumber␈αof␈αelements.␈αThe␈α
value
␈↓ ↓H␈↓returned␈αis␈αto␈αbe␈αa␈αlist␈αof␈αdotted␈αpairs;␈αthe␈αelements␈αof␈αthe␈αpairs␈αare␈αthe␈αcorresponding␈αelements␈αof
␈↓ ↓H␈↓the input lists. Thus:

␈↓ ↓H␈↓αdotem[x;y] <= [␈↓ βλnull[x] → ( );
␈↓ ↓H␈↓α␈↓ βλ␈↓
t␈↓α → concat[cons[first[x];first[y]];dotem[rest[x];rest[y]]]]


␈↓ ↓H␈↓The␈α∂first␈α∞thing␈α∂to␈α∂note␈α∞is␈α∂the␈α∞use␈α∂of␈α∂both␈α∞␈↓αconcat␈↓␈α∂and␈α∞␈↓αcons␈↓:␈α∂␈↓αconcat␈↓␈α∂is␈α∞used␈α∂to␈α∞build␈α∂the␈α∂final␈α∞list
␈↓ ↓H␈↓output;␈α␈↓αcons␈↓␈α
is␈αused␈α
to␈αbuild␈αthe␈α
dotted␈αpairs.␈α
Now␈αif␈α
we␈αhad␈αwritten␈α
␈↓αdotem␈↓␈αsuch␈α
that␈αit␈αknew␈α
about
␈↓ ↓H␈↓our␈α∞representation␈α∞of␈α∞lists,␈α∞then␈α∞␈↓↓both␈↓␈α∞functions␈α∞would␈α∞have␈α∞been␈α∞␈↓αcons␈↓.␈α∞The␈α∞definition␈α∂would␈α∞not
␈↓ ↓H␈↓have been quite as clear.  Look at a computation as simple as ␈↓αdotem[(A);(B)]␈↓. This will involve

␈↓ ↓H␈↓α␈↓ ¬concat[cons[A;B];dotem[( );( )]]

␈↓ ↓H␈↓α␈↓Now the evaluation of ␈↓αdotem[( );( )]␈↓ returns our needed ␈↓α( )␈↓, giving

␈↓ ↓H␈↓␈↓ ∧↓␈↓αconcat[cons[A;B];( )] = concat[(A . B);( )] = ((A . B))

␈↓ ↓H␈↓If␈α∞the␈α∂termination␈α∞condition␈α∞of␈α∂␈↓αdotem␈↓␈α∞returned␈α∂anything␈α∞other␈α∞than␈α∂␈↓α( )␈↓␈α∞then␈α∂the␈α∞list-construction
␈↓ ↓H␈↓would "get off on the wrong foot" and would not generate a true list.

␈↓ ↓H␈↓As promised on page 49, here is a "direct" definition of ␈↓αreverse␈↓.

␈↓ ↓H␈↓αreverse[x] <=␈↓ βλ[null[x] → ( );
␈↓ ↓H␈↓α␈↓ βλ ␈↓
t␈↓α → append[reverse[rest[x]];concat[first[x];( )]]]

␈↓ ↓H␈↓This␈α∂reversing␈α∂function␈α∂is␈α∂not␈α∂as␈α∂efficient␈α∂as␈α∂the␈α∂previous␈α∂one.␈α∂ Within␈α∂the␈α∂construction␈α⊂of␈α∂the
␈↓ ↓H␈↓reversed␈α∃list␈α∃the␈α∃␈↓αappend␈↓␈α∃function␈α⊗is␈α∃called␈α∃repeatedly.␈α∃You␈α∃should␈α∃evaluate␈α⊗something␈α∃like
␈↓ ↓H␈↓␈↓αreverse[(A B C D)]␈↓ to see the gross inefficiency.
␈↓ ↓H␈↓␈↓↓2.9␈↓ λxBecoming an expert     51␈↓


␈↓ ↓H␈↓It␈α∩␈↓↓is␈↓␈α∩possible␈α∩to␈α∩write␈α∩a␈α∩directly␈α∩recursive␈α∩reversing␈α∩function␈α∩with␈α∩no␈α∩auxiliary␈α∪functions,␈α∩no
␈↓ ↓H␈↓functions␈αother␈α
than␈αthe␈αprimitives,␈α
and␈αno␈αefficiency.␈α
We␈αshall␈αdo␈α
so␈αsimply␈αbecause␈α
it␈αis␈α
a␈αgood
␈↓ ↓H␈↓example␈αof␈αthe␈αprocess␈αof␈αdiscovering␈αthe␈αgeneral␈αcase␈αof␈αthe␈αrecursion␈αby␈αcareful␈αconsideration␈αof
␈↓ ↓H␈↓examples. Let us call the function ␈↓αrev␈↓.

␈↓ ↓H␈↓Let's␈α⊂worry␈α⊂about␈α⊂the␈α⊂termination␈α⊂conditions␈α⊂later.␈α⊂Consider,␈α⊂for␈α⊂example,␈α⊂␈↓αrev[(A B C D)]␈↓.␈α∂This
␈↓ ↓H␈↓should␈α⊂evaluate␈α⊂to␈α⊂␈↓α(D C B A)␈↓.␈α∂How␈α⊂can␈α⊂we␈α⊂construct␈α∂this␈α⊂list␈α⊂by␈α⊂recursive␈α∂calls␈α⊂on␈α⊂␈↓αrev␈↓?␈α⊂In␈α∂the
␈↓ ↓H␈↓following,␈α≠assume␈α~␈↓αx␈↓␈α≠has␈α≠value␈α~␈↓α(A B C D)␈↓.␈α≠ Now␈α~note␈α≠that␈α≠␈↓α(D C B A)␈↓␈α~is␈α≠the␈α≠value␈α~of
␈↓ ↓H␈↓␈↓αconcat[D;(C B A)]␈↓.␈α
Then␈α␈↓αD␈↓␈α
is␈α
␈↓αfirst[rev[rest[x]]]␈↓␈α(it␈α
is␈α
also␈α␈↓αfirst[rev[x]]␈↓␈α
but␈α
that␈αwould␈α
not␈α
help␈αus).
␈↓ ↓H␈↓How can we get ␈↓α(C B A)␈↓?   Well:

␈↓ ↓H␈↓α␈↓ ∧λ(C B A)␈↓ ¬_= rev[(A B C)]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[A;(B C)]]   ␈↓(we are going after ␈↓αrest[x]␈↓ again)␈↓α
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_                        ␈↓but first  we can get ␈↓αA␈↓ from ␈↓αx.
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];(B C)]]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];rev[(C B)]]]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];rev[rest[(D C B)]]]]
␈↓ ↓H␈↓α␈↓ ∧λ␈↓ ¬_= rev[concat[first[x];rev[rest[rev[rest[x]]]]]]

␈↓ ↓H␈↓α␈↓Finally␈↓α
␈↓ ↓H␈↓α␈↓ αwrev[x] = concat[first[rev[rest[x]]];rev[concat[first[x];rev[rest[rev[rest[x]]]]]]]

␈↓ ↓H␈↓The␈α∞termination␈α
conditions␈α∞are␈α∞simple.␈α
First␈α∞␈↓αrev[( )]␈↓␈α∞gives␈α
␈↓α( )␈↓.␈α∞ Then␈α∞notice␈α
that␈α∞the␈α∞general␈α
case
␈↓ ↓H␈↓which␈α
we␈α
just␈α
constructed␈α
has␈α
␈↓↓two␈↓␈α
␈↓αconcat␈↓s.␈α
 That␈α∞means␈α
the␈α
shortest␈α
list␈α
which␈α
it␈α
can␈α
make␈α∞is␈α
of
␈↓ ↓H␈↓length␈αtwo.␈α So␈αlists␈αof␈αlength␈αone␈αare␈αhandled␈αseparately:␈αthe␈αreverse␈αof␈αsuch␈αa␈αlist␈αis␈αitself.␈α Thus
␈↓ ↓H␈↓the complete definition should be:

␈↓ ↓H␈↓αrev[x] <= [␈↓ αXnull[x] → ( );
␈↓ ↓H␈↓α␈↓ αXnull[rest[x]] → x;
␈↓ ↓H␈↓α␈↓ αX␈↓
t␈↓α → concat[first[rev[rest[x]]];rev[concat[first[x];rev[rest[rev[rest[x]]]]]]] ]



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓I  Use the following definition:
␈↓ ↓H␈↓α␈↓ αXmatch[k;m] <=␈↓ ∧([null[k] → NO;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( null[m] → NO;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( eq[first[k];first[m]] → first[k];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( ␈↓
t␈↓α → match[rest[k];rest[m]]]

␈↓ ↓H␈↓and evaluate:
␈↓ ↓H␈↓␈↓↓1.␈↓α match[(X);(X)]   ␈↓↓2.␈↓α match[(A B E);(J O E)]  ␈↓↓3.␈↓α match[(F O O); (BAZ)]
␈↓ ↓H␈↓␈↓↓52  Symbolic expressions␈↓ 52.9␈↓



␈↓ ↓H␈↓II Now write your own functions:

␈↓ ↓H␈↓␈↓↓1.␈↓α␈α
among[x;y]␈α
<=␈α
...␈α
:␈α
among␈↓␈α
is␈αto␈α
be␈α
a␈α
predicate;␈α
␈↓αx␈↓␈α
is␈α
an␈α
atom;␈α␈↓αy␈↓␈α
is␈α
a␈α
list␈α
of␈α
atoms.␈α
 ␈↓αamong␈↓␈α
is␈αto␈α
return
␈↓ ↓H␈↓␈↓ α8␈↓
f␈↓ if ␈↓αx␈↓ is not found as an element of ␈↓αy␈↓; otherwise, ␈↓αamong␈↓ is to return ␈↓
t␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αamong[A;(A B C)] = among[A;(C D E A)] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ αX     among[A1;(A2 B2)] = ␈↓
f␈↓α.


␈↓ ↓H␈↓␈↓↓2.␈↓α␈α⊂anywhere[x;y]␈α⊂<=␈α∂...␈α⊂:␈α⊂anywhere␈↓␈α⊂is␈α∂a␈α⊂predicate;␈α⊂␈↓αx␈↓␈α⊂is␈α∂an␈α⊂atom;␈α⊂␈↓αy␈↓␈α⊂is␈α∂an␈α⊂arbitrary␈α⊂S-expr␈α⊂or␈α∂list.
␈↓ ↓H␈↓␈↓ α8␈↓αanywhere␈↓ is to return ␈↓
t␈↓ just in the case that ␈↓αx␈↓ appears somewhere in ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αanywhere[A;(A B C)] = anywhere[A;((A . B). C)] = ␈↓
t␈↓α
␈↓ ↓H␈↓α␈↓ αX     anywhere[A;(B C D)] = ␈↓
f␈↓α.


␈↓ ↓H␈↓␈↓↓3.␈↓α␈α∪collectpair[z;x;y]␈α∀<=␈α∪...␈α∀:␈α∪x␈↓␈α∀and␈α∪␈↓αy␈↓␈α∀are␈α∪atoms;␈α∪␈↓αz␈↓␈α∀is␈α∪an␈α∀S-expression␈α∪or␈α∀list,␈α∪some␈α∀of␈α∪whose
␈↓ ↓H␈↓␈↓ α8subexpressions,␈α⊂may␈α∂begin␈α⊂␈↓α(x␈α⊂...)␈↓␈α∂or␈α⊂␈↓α(y␈α∂...)␈↓.␈α⊂ ␈↓αcollectpair␈↓␈α⊂is␈α∂to␈α⊂return␈α∂a␈α⊂dotted␈α⊂pair␈α∂whose
␈↓ ↓H␈↓␈↓ α8␈↓αcar␈↓-part␈α
is␈α
a␈αlist␈α
of␈α
all␈α
the␈αoccurrences␈α
of␈α
␈↓α(x...)␈↓␈αand␈α
whose␈α
␈↓αcdr␈↓-part␈α
is␈αa␈α
list␈α
of␈αall␈α
occurrences
␈↓ ↓H␈↓␈↓ α8of ␈↓α(y ...)␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αcollectpair[((A 1)((B . 2)(C A 4)));A;B] = (((A 1)(A 4)).((B . 2)))


␈↓ ↓H␈↓␈↓↓4.␈↓α␈α⊃pred[x]␈α⊃<=␈α⊃...␈α⊃:␈α∩x␈↓␈α⊃is␈α⊃a␈α⊃positive␈α⊃integer.␈α⊃␈↓αpred␈↓␈α∩is␈α⊃a␈α⊃function,␈α⊃returning␈α⊃the␈α⊃predecessor␈α∩of␈α⊃its
␈↓ ↓H␈↓␈↓ α8argument. The only arithmetic function you may use is ␈↓αadd1␈↓.

␈↓ ↓H␈↓␈↓ αXe.g. ␈↓αpred[3] = 2;  pred[0] ␈↓is undefined␈↓α;
␈↓ ↓H␈↓α␈↓ αX     pred[add1[x]] = x ␈↓ for ␈↓αx ␈↓≥␈↓α 0.␈↓


␈↓ ↓H␈↓␈↓↓5.␈↓α␈α
signum[x]␈α
<=␈α
...:␈α
x␈↓␈α
is␈α
an␈α
integer.␈α
␈↓αsignum␈↓␈α
returns␈α
␈↓αNEGATIVE␈↓,␈α
␈↓αZERO␈↓,␈α
or␈α
␈↓αPOSITIVE␈↓␈αdepending
␈↓ ↓H␈↓␈↓ α8on the sign of ␈↓αx␈↓. You may use ␈↓αadd1␈↓ and ␈↓αsub1␈↓ but no comparision function other than ␈↓αeq␈↓.

␈↓ ↓H␈↓␈↓↓6.␈↓α␈α∂maxdepth[l]␈α∂<=␈α∞...:␈α∂l␈↓␈α∂is␈α∞a␈α∂list.␈α∂This␈α∞function␈α∂is␈α∂to␈α∞find␈α∂the␈α∂maximum␈α∞depth␈α∂of␈α∂nesting␈α∂of␈α∞any
␈↓ ↓H␈↓␈↓ α8element␈α
in␈α
␈↓αl␈↓.␈α
Assume␈α
that␈α
␈↓αl␈↓␈α
is␈α
a␈α∞strict␈α
list␈α
(see␈α
page 38);␈α
that␈α
is,␈α
any␈α
sub-element␈α∞is␈α
either
␈↓ ↓H␈↓␈↓ α8atomic or is itself a strict list.  For example ␈↓αmaxdepth[( )] = 0; maxdepth[(((B) C) A)] = 3.␈↓
␈↓ ↓H␈↓␈↓↓3.␈↓ 	\Applications     53␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 3

␈↓ ↓H␈↓↓␈↓ ¬→APPLICATIONS OF LISP␈↓




␈↓ ↓H␈↓␈↓ ∧λ"...All␈αthe␈αtime␈αI␈α
design␈αprograms␈αfor␈αnonexisting␈α
machines␈αand␈αadd:␈α`if␈α
we
␈↓ ↓H␈↓␈↓ ∧λnow␈αhad␈αa␈αmachine␈αcomprising␈αthe␈αprimitives␈αhere␈αassumed,␈αthen␈αthe␈αjob
␈↓ ↓H␈↓␈↓ ∧λis done.'

␈↓ ↓H␈↓␈↓ ∧λ...␈αIn␈α
actual␈αpractice,␈α
of␈αcourse,␈αthis␈α
ideal␈αmachine␈α
will␈αturn␈α
out␈αnot␈αto␈α
exist,
␈↓ ↓H␈↓␈↓ ∧λso␈α∞our␈α∞next␈α∞task␈α∞--structurally␈α∞similar␈α∞to␈α∞the␈α∞original␈α∞one--␈α∞is␈α∞to␈α∞program
␈↓ ↓H␈↓␈↓ ∧λthe␈α∂simulation␈α∂of␈α∂the␈α∂"upper"␈α∞machine....␈α∂But␈α∂this␈α∂bunch␈α∂of␈α∂programs␈α∞is
␈↓ ↓H␈↓␈↓ ∧λwritten␈αfor␈αa␈αmachine␈αthat␈αin␈αall␈αprobability␈αwill␈αnot␈αexist,␈αso␈αour␈αnext␈αjob
␈↓ ↓H␈↓␈↓ ∧λwill␈αbe␈αto␈αsimulate␈αit␈αin␈αterms␈αof␈αprograms␈αfor␈αa␈αnext␈αlower␈αlevel␈αmachine,
␈↓ ↓H␈↓␈↓ ∧λetc.,␈α∃until␈α∃finally␈α⊗we␈α∃have␈α∃a␈α∃program␈α⊗that␈α∃can␈α∃be␈α∃executed␈α⊗by␈α∃our
␈↓ ↓H␈↓␈↓ ∧λhardware...."

␈↓ ↓H␈↓␈↓ εQE. W. Dijkstra, ␈↓αNotes on Structured Programming␈↓ 



␈↓ ↓H␈↓␈↓ ¬`␈↓↓3.1  Introduction␈↓


␈↓ ↓H␈↓There␈α
are␈α
at␈α
least␈α
two␈αways␈α
of␈α
interpreting␈α
this␈α
remark␈α
of␈αDijkstra.␈α
 At␈α
the␈α
immediate␈α
level␈αwe␈α
note
␈↓ ↓H␈↓that␈α∞anyone␈α
who␈α∞has␈α
programmed␈α∞at␈α∞a␈α
level␈α∞higher␈α
than␈α∞machine␈α
language␈α∞has␈α∞experienced␈α
the
␈↓ ↓H␈↓phenomenon.␈α∞For␈α∂the␈α∞natural␈α∂interpretation␈α∞of␈α∂programming␈α∞in␈α∂a␈α∞high-level␈α∂language␈α∞is␈α∂that␈α∞of
␈↓ ↓H␈↓␈↓↓writing␈↓␈α⊂algorithms␈α⊃for␈α⊂the␈α⊂non-existing␈α⊃high-level␈α⊂machine.␈α⊂Typically,␈α⊃however␈α⊂the␈α⊃changes␈α⊂of
␈↓ ↓H␈↓representation␈α∞from␈α∞machine␈α∞to␈α∞machine␈α∂are␈α∞all␈α∞done␈α∞automatically:␈α∞from␈α∞high-level,␈α∂to␈α∞assembly
␈↓ ↓H␈↓language, and finally to hardware instructions.

␈↓ ↓H␈↓The␈αmore␈αfruitful␈αview␈αof␈αDijkstra's␈αremark␈α
is␈αrelated␈αto␈αour␈αdiscussions␈αof␈αabstract␈αdata␈α
structures
␈↓ ↓H␈↓and␈αalgorithms.␈αIn␈α
this␈αview␈αwe␈αexpress␈α
our␈αalgorithms␈αand␈αdata␈α
structures␈αin␈αterms␈αof␈α
abstractions
␈↓ ↓H␈↓independent␈α∩of␈α⊃how␈α∩they␈α∩may␈α⊃be␈α∩represented␈α∩in␈α⊃a␈α∩machine;␈α⊃indeed␈α∩we␈α∩can␈α⊃use␈α∩the␈α∩ideas␈α⊃of
␈↓ ↓H␈↓abstraction␈α␈↓↓regardless␈↓␈αof␈αwhether␈αthe␈αformalism␈αwill␈αfind␈αa␈αrepresentation␈αon␈αa␈αmachine.␈αThis␈αuse
␈↓ ↓H␈↓of␈αabstraction␈αis␈αthe␈αtrue␈αsense␈αof␈α
the␈αprogramming␈αstyle␈αcalled␈α"structured␈αprogramming".␈α We␈α
will
␈↓ ↓H␈↓see␈α"in␈α"this␈α"chapter␈α"how␈α"this␈α"programming␈α"style␈α"is␈α"a␈α"natural␈α"result␈α#of␈α"writing
␈↓ ↓H␈↓representation-independent LISP programs.

␈↓ ↓H␈↓As␈α⊂we␈α⊃have␈α⊂previously␈α⊃remarked,␈α⊂we␈α⊂will␈α⊃see␈α⊂a␈α⊃close␈α⊂relationship␈α⊂between␈α⊃the␈α⊂structure␈α⊃of␈α⊂an
␈↓ ↓H␈↓algorithm␈αand␈α
the␈αstructure␈αof␈α
the␈αdata.␈α We␈α
have␈αseen␈α
this␈αalready␈αon␈α
a␈αsmall␈αscale:␈α
list-algorithms
␈↓ ↓H␈↓recur␈α
"linearly"␈α
on␈α
␈↓αrest␈↓␈α
to␈α
␈↓α( )␈↓;␈α
S-expr␈α
algorithms␈α
recur␈α
"left-and-right"␈α
on␈α
␈↓αcar␈↓␈α
and␈α
␈↓αcdr␈↓␈α
to␈α∞an␈α
atom.
␈↓ ↓H␈↓Indeed,␈α
the␈αinstances␈α
of␈αcontrol␈α
structures␈αappearing␈α
in␈αan␈α
algorithm␈αtypically␈α
parallel␈αthe␈α
style␈αof
␈↓ ↓H␈↓␈↓↓54  Applications␈↓ 73.1␈↓


␈↓ ↓H␈↓inductive␈α⊃definition␈α⊃of␈α∩the␈α⊃data␈α⊃structure␈α∩which␈α⊃the␈α⊃algorithm␈α⊃is␈α∩examining.␈α⊃If␈α⊃a␈α∩structure␈α⊃is
␈↓ ↓H␈↓defined as:
␈↓ ↓H␈↓␈↓ ¬Z␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ | ␈↓
D␈↓β2␈↓ | ␈↓
D␈↓β3␈↓
␈↓ ↓H␈↓␈↓ ∧oe.g.  <seq elem> ::= <indiv> | <seq>

␈↓ ↓H␈↓then␈α∂we␈α∂can␈α∂expect␈α∂to␈α∂find␈α∂a␈α∂conditional␈α∂expression␈α∂whose␈α∂predicate␈α∂positions␈α∂are␈α∂filled␈α∂by␈α∞the
␈↓ ↓H␈↓recognizers for the ␈↓
D␈↓βi␈↓'s. If the structure is defined as:
␈↓ ↓H␈↓␈↓ ¬o␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ...  ␈↓
D␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ∧Be.g.   <seq> ::= ␈↓↓(␈↓<seq elem>␈↓↓,␈↓ ..., <seq elem>␈↓↓)␈↓

␈↓ ↓H␈↓that␈α⊃is,␈α⊃a␈α⊃homogeneous␈α⊃sequence␈α⊂of␈α⊃elements,␈α⊃then␈α⊃we␈α⊃will␈α⊂have␈α⊃a␈α⊃"linear"␈α⊃recursion␈α⊃like␈α⊂that
␈↓ ↓H␈↓experienced␈α∪in␈α∩list-algorithms␈↓π 37␈↓.␈α∪ Finally␈α∪if␈α∩the␈α∪structure␈α∩is␈α∪defined␈α∪with␈α∩a␈α∪fixed␈α∪number␈α∩of
␈↓ ↓H␈↓components as:
␈↓ ↓H␈↓␈↓ ¬K␈↓
D␈↓ ::= ␈↓
D␈↓β1␈↓ ␈↓
D␈↓β2␈↓ ␈↓
D␈↓β3␈↓... ␈↓
D␈↓βn␈↓
␈↓ ↓H␈↓␈↓ ∧Ze.g.     <sexpr> ::= ␈↓α(␈↓<sexpr> . <sexpr>␈↓α)␈↓

␈↓ ↓H␈↓then we can expect a full recursion like that of S-exprs␈↓π 38␈↓.

␈↓ ↓H␈↓Thus␈αa␈αdata-structure␈αalgorithm␈αtends␈αto␈α"pass␈αoff"␈αits␈αwork␈αto␈αsubfunctions␈αwhich␈αwill␈αoperate␈αon
␈↓ ↓H␈↓the␈αcomponents␈α
of␈αthe␈αdata␈α
structure.␈αThus␈αif␈α
a␈αstructure␈αof␈α
type␈α␈↓
D␈↓␈αis␈α
made␈αup␈αof␈α
components␈αof
␈↓ ↓H␈↓types␈α
␈↓
D␈↓β1␈↓,␈α
␈↓
D␈↓β2␈↓,␈α␈↓
D␈↓β3␈↓,␈α
and␈α
␈↓
D␈↓β4␈↓,␈αthen␈α
the␈α
structure␈α
of␈αan␈α
algorithm␈α
␈↓αf␈↓␈αoperating␈α
on␈α
␈↓
D␈↓␈α
typically␈αinvolves
␈↓ ↓H␈↓calls␈α
on␈α
subfunctions␈α␈↓αf␈↓β1␈↓␈α
through␈α
␈↓αf␈↓β4␈↓␈αto␈α
handle␈α
the␈α
subcomputations.␈αEach␈α
␈↓αf␈↓βi␈↓␈α
will␈αin␈α
turn␈α
break␈αup␈α
its
␈↓ ↓H␈↓␈↓
D␈↓βi␈↓.

␈↓ ↓H␈↓Thus the type-structure of the call on ␈↓αf␈↓ would be:

␈↓ ↓H␈↓␈↓ ∧]␈↓αf[␈↓
D␈↓α] = g[f␈↓β1␈↓α[␈↓
D␈↓β1␈↓α];f␈↓β2␈↓α[␈↓
D␈↓β2␈↓α];f␈↓β3␈↓α[␈↓
D␈↓β3␈↓α];f␈↓β4␈↓α[␈↓
D␈↓β4␈↓α]]

␈↓ ↓H␈↓This␈α↔is␈α⊗the␈α↔essence␈α⊗of␈α↔level-wise␈α⊗programming:␈α↔we␈α⊗write␈α↔␈↓αf,␈α⊗f␈↓β1␈↓α, ... ,f␈↓β4␈↓␈α↔independently␈α↔of␈α⊗the
␈↓ ↓H␈↓representation␈αof␈αtheir␈αdata␈αstructures.␈α ␈↓αf␈↓␈αwill␈αrun␈αprovided␈αthat␈αthe␈α␈↓αf␈↓βi␈↓'s␈αare␈αavailable.␈αAs␈αwe␈αwrite
␈↓ ↓H␈↓the␈α⊃␈↓αf␈↓βi␈↓'s␈α⊃we␈α∩will␈α⊃probably␈α⊃invoke␈α∩computations␈α⊃on␈α⊃components␈α∩of␈α⊃the␈α⊃corresponding␈α∩␈↓
D␈↓βi␈↓.␈α⊃Those
␈↓ ↓H␈↓computations␈α∩are␈α∩in␈α∩turn␈α∩executed␈α∩by␈α∩subfunctions␈α∩which␈α∩we␈α∩have␈α∩to␈α∩write.␈α∩This␈α∪process␈α∩of
␈↓ ↓H␈↓elaboration␈α⊃terminates␈α⊂when␈α⊃all␈α⊂subfunctions␈α⊃are␈α⊂written␈α⊃and␈α⊂all␈α⊃data␈α⊂structures␈α⊃have␈α⊂received
␈↓ ↓H␈↓concrete␈α∞representations.␈α∞In␈α
LISP␈α∞this␈α∞means␈α∞the␈α
lowest␈α∞level␈α∞functions␈α
are␈α∞expressed␈α∞in␈α∞terms␈α
of
␈↓ ↓H␈↓LISP␈α
primitives␈α
and␈α
the␈α
data␈α
structures␈α
are␈α
represented␈α
in␈α
terms␈α
of␈α
S-exprs.␈α
Thus␈α
at␈α
the␈αhighest
␈↓ ↓H␈↓level␈αwe␈αtend␈αto␈αthink␈αof␈αa␈αdata␈αstructure␈αas␈αa␈αclass␈αof␈αbehaviors;␈αwe␈αdon't␈αcare␈αabout␈αthe␈αinternal
␈↓ ↓H␈↓mechanisms␈α∀which␈α∀implement␈α∪that␈α∀behavior.␈α∀ At␈α∪the␈α∀lowest␈α∀level,␈α∀machine-language␈α∪routines
␈↓ ↓H␈↓simulate ␈↓↓one␈↓ of many possible representations.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 37␈↓␈αIndeed␈α
there␈αare␈αother␈α
forms␈αof␈α
control␈αlike␈αiteration␈α
or␈α␈↓αlit␈↓␈α(page 146)␈α
which␈αare␈α
more␈αnatural
␈↓ ↓H␈↓for such data structures.

␈↓ ↓H␈↓␈↓π 38␈↓␈α
You␈α∞should␈α
have␈α∞noticed␈α
that␈α∞we␈α
are␈α∞therefore␈α
dealing␈α∞with␈α
essentially␈α∞"context-free"␈α
abstract
␈↓ ↓H␈↓data structures; i.e., those generated by context-free grammars.
␈↓ ↓H␈↓␈↓↓3.1␈↓ 	↑Introduction     55␈↓


␈↓ ↓H␈↓This␈α∞process␈α∞of␈α∞elaboration␈α
of␈α∞abstract␈α∞algorithm␈α∞and␈α
abstract␈α∞data␈α∞structure␈α∞does␈α∞not␈α
invalidate
␈↓ ↓H␈↓the␈α∀top-level␈α∪definition␈α∀of␈α∪␈↓αf␈↓␈α∀it␈α∪remains␈α∀intact.␈α∪ We␈α∀should␈α∪note,␈α∀however,␈α∪that␈α∀this␈α∀style␈α∪of
␈↓ ↓H␈↓programming␈α⊂is␈α∂not␈α⊂a␈α⊂panacea;␈α∂it␈α⊂is␈α⊂no␈α∂substitute␈α⊂for␈α∂clear␈α⊂thinking.␈α⊂ It␈α∂only␈α⊂helps␈α⊂control␈α∂the
␈↓ ↓H␈↓complexity of the programming process. With this in mind, here are some programming examples.



␈↓ ↓H␈↓␈↓ ∧`␈↓↓3.2  Examples of LISP applications␈↓


␈↓ ↓H␈↓The␈α⊂next␈α⊂few␈α⊂sections␈α⊂will␈α⊂examine␈α⊂some␈α⊂non-trivial␈α⊂problems␈α⊂involving␈α⊂computations␈α⊂on␈α∂data
␈↓ ↓H␈↓structures.␈α We␈αwill␈αdescribe␈αthe␈αproblem␈αintuitively,␈αpick␈αan␈αinitial␈αrepresentation␈αfor␈αthe␈αproblem,
␈↓ ↓H␈↓write␈αthe␈αLISP␈αalgorithm,␈αand␈αin␈αsome␈αcases␈α"tune"␈αthe␈αalgorithm␈αby␈αpicking␈α"more␈αefficient"␈αdata
␈↓ ↓H␈↓representations.

␈↓ ↓H␈↓The examples share other important characteristics:

␈↓ ↓H␈↓␈↓↓1.␈↓ We examine the problem domain and attempt to represent its elements as data structures.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α∩We␈α⊃reflect␈α∩on␈α∩our␈α⊃(intuitive)␈α∩algorithm␈α⊃and␈α∩try␈α∩to␈α⊃express␈α∩it␈α⊃as␈α∩a␈α∩LISP-like␈α⊃data-structure
␈↓ ↓H␈↓manipulating function.

␈↓ ↓H␈↓␈↓↓3.␈↓␈αWhile␈αperforming␈α␈↓↓1␈↓␈αand␈α␈↓↓2␈↓,␈αwe␈αmight␈αhave␈αto␈αmodify␈αsome␈αof␈αour␈αdecisions.␈αSomething␈αassumed
␈↓ ↓H␈↓to be structure might better be represented as algorithm, or the converse might be the case.

␈↓ ↓H␈↓␈↓↓4.␈↓ When the decisions are made, we evaluate the LISP function on a representation of a problem.

␈↓ ↓H␈↓␈↓↓5.␈↓ We reinterpret the data-structure output as an answer to our problem.

␈↓ ↓H␈↓Pictorially in terms of LISP:

␈↓ ↓H␈↓intuitive algorithm => LISP function␈↓ ¬h|
␈↓ ↓H␈↓␈↓ ¬h|  evaluation
␈↓ ↓H␈↓␈↓ ¬h|==============> interpret S-expr output as answer
␈↓ ↓H␈↓␈↓ ¬h|
␈↓ ↓H␈↓problem domain      => S-expressions␈↓ ¬h|


␈↓ ↓H␈↓Whenever␈αwe␈α
write␈αcomputer␈αprograms,␈α
whatever␈αlanguage␈αwe␈α
use,␈αwe␈αalways␈α
go␈αthrough␈αa␈α
similar
␈↓ ↓H␈↓representation␈αproblem.␈αThe␈αprocess␈αis␈αmore␈αapparent␈αin␈αa␈αhigher-level␈αlanguage␈αlike␈αFORTRAN
␈↓ ↓H␈↓or␈α⊃ALGOL,␈α⊂and␈α⊃is␈α⊂most␈α⊃noticeable␈α⊂in␈α⊃a␈α⊂language␈α⊃like␈α⊂LISP␈α⊃which␈α⊂primarily␈α⊃deals␈α⊃with␈α⊂data
␈↓ ↓H␈↓structures.

␈↓ ↓H␈↓When␈α∞we␈α
deal␈α∞with␈α
numerical␈α∞algorithms,␈α
the␈α∞representation␈α
problem␈α∞has␈α
usually␈α∞been␈α∞settled␈α
in
␈↓ ↓H␈↓the␈α⊃transformation␈α∩from␈α⊃real-world␈α∩situation␈α⊃to␈α∩a␈α⊃numerical␈α∩problem.␈α⊃One␈α∩has␈α⊃to␈α∩think␈α⊃more
␈↓ ↓H␈↓␈↓↓56  Applications␈↓ 53.2␈↓


␈↓ ↓H␈↓explicitly␈α∪about␈α∩representation␈α∪when␈α∪we␈α∩deal␈α∪with␈α∩structures␈α∪like␈α∪arrays␈α∩or␈α∪matrices.␈α∪We␈α∩are
␈↓ ↓H␈↓encoding␈αour␈α
information␈αin␈α
the␈αarray.␈αBut␈α
the␈αpreceding␈α
diagram␈αoccurs␈α
within␈αthe␈αmachine,␈α
even
␈↓ ↓H␈↓for strictly non-structured numerical calculation.

␈↓ ↓H␈↓numerical algorithm => machine instructions␈↓ ε8|
␈↓ ↓H␈↓␈↓ ε8|  execution
␈↓ ↓H␈↓␈↓ ε8|==========> interpret binary number as answer
␈↓ ↓H␈↓␈↓ ε8|
␈↓ ↓H␈↓numbers => binary representation␈↓ ε8|


␈↓ ↓H␈↓The␈α∂encodings␈α⊂are␈α∂done␈α⊂by␈α∂the␈α⊂input␈α∂routines.␈α∂The␈α⊂result␈α∂of␈α⊂the␈α∂execution␈α⊂is␈α∂presented␈α⊂to␈α∂the
␈↓ ↓H␈↓external world by the output routines.

␈↓ ↓H␈↓However,␈α∞it␈α∂is␈α∞not␈α∞until␈α∂we␈α∞come␈α∞to␈α∂data-structure␈α∞computations,␈α∞or␈α∂non-numerical␈α∞computations,
␈↓ ↓H␈↓that␈αthe␈α
representation␈αproblem␈αreally␈α
becomes␈αundeniable.␈α We␈α
have␈αto␈αthink␈α
more␈αabout␈αwhat␈α
we
␈↓ ↓H␈↓are␈α∀doing␈α∪since␈α∀we␈α∪lack␈α∀certain␈α∀preconceptions␈α∪or␈α∀intuitions␈α∪about␈α∀such␈α∀computations.␈α∪More
␈↓ ↓H␈↓importantly,␈αhowever,␈αwe␈α
are␈αtrying␈αto␈α
represent␈αactual␈αproblems␈α
␈↓↓directly␈↓␈αas␈αmachine␈αproblems.␈α
We
␈↓ ↓H␈↓do␈αnot␈αattempt␈αto␈αfirst␈αanalyze␈αthem␈αinto␈αa␈αcomplex␈αmathematical␈αtheory,␈αbut␈αshould␈αtry␈αto␈αexpress
␈↓ ↓H␈↓our␈α∞intuitive␈α∞theory␈α∞directly␈α∞as␈α∞data-structure␈α∞computation.␈α∞ This␈α∞is␈α∞a␈α∞different␈α∞kind␈α∞of␈α
thinking,
␈↓ ↓H␈↓due␈αwholly␈αto␈αthe␈αadvent␈αof␈αcomputers.␈α Indeed␈αthe␈αfield␈αof␈αcomputation␈αhas␈αexpanded␈αso␈αmuch␈αas
␈↓ ↓H␈↓to␈α⊂obsolete␈α⊂the␈α⊂term␈α⊂"computer".␈α⊂"Structure␈α⊂processor"␈α∂is␈α⊂more␈α⊂indicative␈α⊂of␈α⊂the␈α⊂proper␈α⊂level␈α∂at
␈↓ ↓H␈↓which we should view "computers".

␈↓ ↓H␈↓We␈α∪have␈α∪already␈α∩seen␈α∪a␈α∪simple␈α∪example␈α∩of␈α∪the␈α∪representation␈α∩problem␈α∪in␈α∪the␈α∪discussion␈α∩of
␈↓ ↓H␈↓list-notation beginning in Section 2.6.

␈↓ ↓H␈↓list algorithm => LISP function␈↓ ¬h|
␈↓ ↓H␈↓␈↓ ¬h|  evaluation
␈↓ ↓H␈↓␈↓ ¬h|============> re-interpret S-expr result as list-output.
␈↓ ↓H␈↓␈↓ ¬h|
␈↓ ↓H␈↓list expression => S-expression␈↓ ¬h|


␈↓ ↓H␈↓The following sections deal with representation of complex data structure problems in LISP.



␈↓ ↓H␈↓␈↓ ¬M␈↓↓3.3  Differentiation␈↓


␈↓ ↓H␈↓This␈α∀example␈α∀will␈α∀describe␈α∀a␈α∀rudimentary␈α∀differentiation␈α∀routine␈α∀for␈α∀polynomials␈α∀in␈α∀several
␈↓ ↓H␈↓variables.␈α
 We␈α
will␈α
develop␈α
this␈αalgorithm␈α
through␈α
several␈α
stages.␈α
We␈αwill␈α
begin␈α
by␈α
doing␈α
a␈αvery
␈↓ ↓H␈↓direct,␈αbut␈αrepresentation-dependent,␈αimplementation.␈α We␈αwill␈αencode␈αpolynomials␈αas␈α
special␈αLISP
␈↓ ↓H␈↓lists␈α⊗and␈α⊗will␈α⊗express␈α⊗the␈α⊗differentiation␈α∃algorithm␈α⊗as␈α⊗a␈α⊗LISP␈α⊗program␈α⊗operating␈α⊗on␈α∃that
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     57␈↓


␈↓ ↓H␈↓representation.␈α When␈αthis␈αprogram␈αis␈αcompletely␈αspecified␈αwe␈αwill␈αthen␈αscrutinize␈αit,␈αattempting␈αto
␈↓ ↓H␈↓see␈αjust␈αhow␈αmuch␈αof␈αthe␈αprogram␈αand␈αdata␈αstructure␈αis␈αrepresentation␈αand␈αhow␈αmuch␈αis␈αessential
␈↓ ↓H␈↓to the expression of the algorithm.

␈↓ ↓H␈↓You␈α⊃should␈α∩recognize␈α⊃two␈α⊃facts␈α∩about␈α⊃the␈α⊃differentiation␈α∩algorithm␈α⊃for␈α⊃polynomials:␈α∩First,␈α⊃the
␈↓ ↓H␈↓algorithm␈α
operates␈α
on␈αforms␈α
(or␈α
expressions)␈αas␈α
arguments␈α
and␈αreturns␈α
forms␈α
as␈α
values.␈αTypically
␈↓ ↓H␈↓we␈α⊃think␈α⊃of␈α∩the␈α⊃arguments␈α⊃and␈α∩values␈α⊃to␈α⊃functions␈α∩as␈α⊃being␈α⊃simple␈α∩values,␈α⊃or␈α⊃the␈α∩␈↓↓results␈↓␈α⊃of
␈↓ ↓H␈↓evaluating␈α
complex␈αforms,␈α
not␈αforms␈α
themselves.␈α
 Second,␈αand␈α
more␈αimportant,␈α
you␈α
should␈αrealize
␈↓ ↓H␈↓that␈α⊂the␈α∂algorithm␈α⊂for␈α∂differentiation␈α⊂is␈α∂recursive!␈α⊂ The␈α∂question␈α⊂of␈α∂differentiation␈α⊂of␈α∂a␈α⊂sum␈α∂is
␈↓ ↓H␈↓thrown␈α⊂back␈α⊂on␈α⊂the␈α⊂differentiation␈α⊂of␈α⊂each␈α⊂summand.␈α⊂ Similar␈α⊂relationships␈α⊂hold␈α⊂for␈α∂products,
␈↓ ↓H␈↓differences,␈α∞and␈α∞powers.␈α∞ As␈α∂with␈α∞all␈α∞good␈α∞recursive␈α∂definitions,␈α∞there␈α∞must␈α∞be␈α∂some␈α∞termination
␈↓ ↓H␈↓conditions.␈α∞ What␈α
are␈α∞the␈α
termination␈α∞conditions␈α
here?␈α∞ Differentiation␈α
of␈α∞a␈α
variable,␈α∞say␈α∞␈↓αx␈↓,␈α
with
␈↓ ↓H␈↓respect␈α
to␈α
␈↓αx␈↓␈α
is␈α
defined␈α
to␈α
be␈α
1;␈α
differentiating␈α
a␈α
constant,␈α
or␈α
a␈α
variable␈α
not␈α
equal␈α
to␈α
␈↓αx␈↓␈α
with␈α
respect␈α
to
␈↓ ↓H␈↓␈↓αx␈↓␈αgives␈αa␈αresult␈αof␈α
zero.␈α This␈αproblem␈αshould␈αbegin␈αto␈α
sound␈αlike␈αthat␈αof␈αthe␈α
␈↓	IND␈↓-definitions␈αof
␈↓ ↓H␈↓sets␈α(in␈αthis␈αcase␈α
the␈αset␈αof␈αpolynomials)␈αand␈α
the␈αassociated␈α␈↓	REC␈↓-definitions␈αof␈αalgorithms␈α
(in␈αthis
␈↓ ↓H␈↓case␈αdifferentiation␈α
of␈αpolynomials).␈α
 If␈αthis␈α
␈↓↓is␈↓␈αthe␈α
mold␈αinto␈α
which␈αour␈α
current␈αproblem␈α
fits,␈αthen
␈↓ ↓H␈↓our␈α∞first␈α∞order␈α
of␈α∞business␈α∞is␈α
to␈α∞give␈α∞an␈α
inductive␈α∞definition␈α∞of␈α
our␈α∞set␈α∞of␈α∞polynomials.␈α
 Though
␈↓ ↓H␈↓polynomials␈α∀can␈α∃be␈α∀arbitrarily␈α∃complex,␈α∀involving␈α∀the␈α∃operations␈α∀of␈α∃addition,␈α∀multiplication,
␈↓ ↓H␈↓negation,␈α⊂and␈α⊃exponentiation,␈α⊂their␈α⊃general␈α⊂format␈α⊃is␈α⊂very␈α⊂simple␈α⊃if␈α⊂they␈α⊃are␈α⊂described␈α⊃in␈α⊂our
␈↓ ↓H␈↓LISP-like␈αnotation␈αwhere␈αthe␈αoperation␈αprecedes␈αits␈αoperands.␈αIf␈αwe␈αassume␈αthat␈αbinary␈αplus,␈αtimes,
␈↓ ↓H␈↓and␈α∞exponentiation␈α
are␈α∞symbolized␈α
by␈α∞+,␈α∞*,␈α
and␈α∞↑,␈α
we␈α∞will␈α∞write␈α
␈↓α+[x;2]␈↓␈α∞instead␈α
of␈α∞the␈α∞usual␈α
infix
␈↓ ↓H␈↓notation ␈↓αx+2␈↓.  The general term for this LISP-like notation is ␈↓↓prefix notation␈↓.

␈↓ ↓H␈↓Here are some examples of infix and prefix representations:

␈↓ ↓H␈↓␈↓ ¬_␈↓↓infix␈↓ πλprefix

␈↓ ↓H␈↓α␈↓ ¬_x*z+2y␈↓ πλ+[*[x;z]; *[2;y]]
␈↓ ↓H␈↓α␈↓ ¬_x*y*z␈↓ πλ*[x;*[y;z]]

␈↓ ↓H␈↓We␈α⊂will␈α⊂now␈α⊃give␈α⊂an␈α⊂inductive␈α⊂definition␈α⊃of␈α⊂the␈α⊂set␈α⊂of␈α⊃polynomials␈α⊂we␈α⊂wish␈α⊂to␈α⊃consider.␈α⊂The
␈↓ ↓H␈↓definition will involve an inductive definition of terms.

␈↓ ↓H␈↓␈↓↓1.␈↓ terms are polynomials.

␈↓ ↓H␈↓␈↓↓2.␈↓ If p␈↓β1␈↓ and p␈↓β2␈↓ are polynomials then the "sum" of p␈↓β1␈↓ and p␈↓β2␈↓ is a polynomial.

␈↓ ↓H␈↓where:

␈↓ ↓H␈↓␈↓↓1.␈↓ constants and variables are terms.

␈↓ ↓H␈↓␈↓↓2.␈↓ If t␈↓β1␈↓ and t␈↓β2␈↓ are terms then the "product" of t␈↓β1␈↓ and t␈↓β2␈↓ is a term.

␈↓ ↓H␈↓␈↓↓3.␈↓ If t␈↓β1␈↓ is a variable and t␈↓β2␈↓ is a constant then "t␈↓β1␈↓ raised to the t␈↓β2␈↓␈↓πth␈↓ power" is a term.

␈↓ ↓H␈↓␈↓↓4.␈↓ If t␈↓β1␈↓ is a term the "minus" t␈↓β1␈↓ is a term.
␈↓ ↓H␈↓␈↓↓58  Applications␈↓ 53.3␈↓


␈↓ ↓H␈↓Armed with prefix notation we can now give a BNF description of the above set:

␈↓ ↓H␈↓<poly>␈↓ βλ::= <term> | <plus>[<poly>;<poly>]

␈↓ ↓H␈↓<term>␈↓ βλ::= <constant> | <variable> | <times>[<term>;<term>] | <expt>[<variable>;<constant>]
␈↓ ↓H␈↓␈↓ βλ::= <minus><term>

␈↓ ↓H␈↓<constant>␈↓ βλ::= <numeral>

␈↓ ↓H␈↓<plus>␈↓ βλ::= +

␈↓ ↓H␈↓<times>␈↓ βλ::= *

␈↓ ↓H␈↓<expt>␈↓ βλ::= ↑

␈↓ ↓H␈↓<minus>␈↓ βλ::= -

␈↓ ↓H␈↓<variable>␈↓ βλ::= <identifier>


␈↓ ↓H␈↓It␈αis␈α
easy␈αto␈α
write␈αrecursive␈α
algorithms␈αin␈αLISP;␈α
the␈αonly␈α
problem␈αis␈α
that␈αthe␈α
domain␈α(and␈αrange)␈α
of
␈↓ ↓H␈↓LISP␈α∂functions␈α∞is␈α∂S-exprs,␈α∂not␈α∞the␈α∂polynomials␈α∂which␈α∞we␈α∂need.␈α∞ So␈α∂we␈α∂need␈α∞a␈α∂way␈α∂to␈α∞represent
␈↓ ↓H␈↓arbitrary␈α
polynomials␈αas␈α
S-exprs.␈α
 We␈αwill␈α
do␈α
the␈αrepresentation␈α
in␈α
lists␈αrather␈α
than␈α
S-exprs.␈α Let␈α
␈↓
R␈↓
␈↓ ↓H␈↓be␈α∞a␈α∞function␈α∞mapping␈α∂polynomals␈α∞to␈α∞their␈α∞representation␈α∞such␈α∂that␈α∞a␈α∞variable␈α∞is␈α∞mapped␈α∂to␈α∞its
␈↓ ↓H␈↓uppercase counterpart in the vocabulary of LISP atoms. Thus:
␈↓ ↓H␈↓␈↓ ∧x␈↓
R␈↓∞(␈↓<variable>␈↓∞)␈↓ = <literal atom>.

␈↓ ↓H␈↓Let constants (numerals), be just the LISP numerals; these are also respectable LISP atoms.  Thus:
␈↓ ↓H␈↓␈↓ ¬␈↓
R␈↓␈↓∞(␈↓<numeral>␈↓∞)␈↓ = <numeral>.

␈↓ ↓H␈↓Since␈α
we␈αhave␈α
now␈α
specified␈αa␈α
representation␈αfor␈α
the␈α
base␈αdomains␈α
of␈αthe␈α
inductive␈α
definition␈αof
␈↓ ↓H␈↓our␈α
polynomials,␈αit␈α
is␈αreasonable␈α
to␈α
think␈αabout␈α
the␈αtermination␈α
cases␈α
for␈αthe␈α
recursive␈αdefinition␈α
of
␈↓ ↓H␈↓differentiation.

␈↓ ↓H␈↓We know from differential calculus that if ␈↓αu␈↓ is a constant or a variable then:
␈↓ ↓H␈↓␈↓ αXd␈↓αu␈↓/d␈↓αx␈↓ =␈↓ βx1 if ␈↓αx = u
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓0 otherwise

␈↓ ↓H␈↓We␈α
will␈α
represent␈α
the␈α∞d-operator␈α
as␈α
a␈α
binary␈α
LISP␈α∞function␈α
named␈α
␈↓αdiff␈↓.␈α
 The␈α∞application,␈α
d␈↓αu␈↓/d␈↓αx␈↓
␈↓ ↓H␈↓will␈αbe␈αrepresented␈αas␈α␈↓αdiff[u;x]␈↓.␈α Now␈αsince␈αconstants␈αand␈αvariables␈αare␈αboth␈αrepresented␈αas␈αatoms,
␈↓ ↓H␈↓we␈αcan␈αcheck␈αfor␈αboth␈α
of␈αthese␈αcases␈αby␈αusing␈αthe␈α
predicate␈α␈↓αisindiv␈↓.␈α Thus␈αa␈αrepresentation␈α
of␈αthe
␈↓ ↓H␈↓termination cases might be:
␈↓ ↓H␈↓␈↓ αX␈↓αdiff[u;x] <= [isindiv[u] → [eq[u;x] → 1; ␈↓
t␈↓α → 0] ....␈↓
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     59␈↓


␈↓ ↓H␈↓Notice␈α∂we␈α∂write␈α∂the␈α∂abbreviation,␈α∂␈↓αisindiv␈↓␈α∂instead␈α∂of␈α∂␈↓αisindiv␈↓βr␈↓.␈α∂ You␈α∂should␈α∂be␈α∂a␈α∂bit␈α∂wary␈α∂of␈α∞our
␈↓ ↓H␈↓definition already: ␈↓αdiff[1;1]␈↓ will evaluate to ␈↓α1␈↓.

␈↓ ↓H␈↓Now␈α∞that␈α
we␈α∞have␈α∞covered␈α
the␈α∞termination␈α∞case,␈α
what␈α∞can␈α
be␈α∞done␈α∞for␈α
the␈α∞representation␈α∞of␈α
the
␈↓ ↓H␈↓remaining class of terms and polynomials? That is, how should we represent sums and products?

␈↓ ↓H␈↓First, we will represent the operations *, +, -, and ↑ as atoms:

␈↓ ↓H␈↓␈↓ ¬`␈↓
R␈↓␈↓∞(␈↓ + ␈↓∞)␈↓ = ␈↓αPLUS␈↓
␈↓ ↓H␈↓␈↓ ¬V␈↓
R␈↓␈↓∞(␈↓ * ␈↓∞)␈↓ = ␈↓αTIMES␈↓
␈↓ ↓H␈↓␈↓ ¬T␈↓
R␈↓␈↓∞(␈↓ - ␈↓∞)␈↓ = ␈↓αMINUS␈↓
␈↓ ↓H␈↓␈↓ ¬]␈↓
R␈↓␈↓∞(␈↓ ↑ ␈↓∞)␈↓ = ␈↓αEXPT␈↓

␈↓ ↓H␈↓We␈α↔will␈α↔now␈α↔extend␈α↔the␈α↔mapping␈α↔␈↓
R␈↓␈α⊗to␈α↔occurrences␈α↔of␈α↔binary␈α↔operators␈α↔by␈α↔mapping␈α⊗to
␈↓ ↓H␈↓three-element lists:
␈↓ ↓H␈↓␈↓ ∧$␈↓
R␈↓␈↓∞(␈↓α ␈↓λα␈↓α[␈↓λβ␈↓β1␈↓α;␈↓λβ␈↓β2␈↓α] ␈↓∞)␈↓ = ␈↓α(␈↓
R␈↓∞(␈↓λα␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓λβ␈↓β1␈↓∞)␈↓, ␈↓
R␈↓∞(␈↓λβ␈↓β2␈↓∞)␈↓α).
␈↓ ↓H␈↓Unary applications will result in two-element lists:

␈↓ ↓H␈↓␈↓ ¬↓␈↓
R␈↓␈↓∞(␈↓α ␈↓λα␈↓α[␈↓λβ␈↓α] ␈↓∞)␈↓ = ␈↓α(␈↓
R␈↓∞(␈↓λα␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓λβ␈↓∞)␈↓α).
␈↓ ↓H␈↓For example:␈↓ ¬∞␈↓
R␈↓∞(␈↓α +[x; 2] ␈↓∞)␈↓ =  ␈↓α(PLUS X 2)␈↓
␈↓ ↓H␈↓A more complicated example: the polynomial,

␈↓ ↓H␈↓α␈↓ εβx␈↓π2␈↓α + 2yz + u
␈↓ ↓H␈↓will be translated to the following prefix notation:

␈↓ ↓H␈↓α␈↓ ¬β+[↑[x;2]; +[*[2;*[y;z]]; u]]     ␈↓π 39␈↓α

␈↓ ↓H␈↓From this it's easy to get the list form:
␈↓ ↓H␈↓α␈↓ βD(PLUS (EXPT X 2)  (PLUS (TIMES 2 (TIMES Y Z)) U))
␈↓ ↓H␈↓Now we can complete the differentiation algorithm for + and *.  We know:

␈↓ ↓H␈↓␈↓ αXd[␈↓αf + g␈↓]/d␈↓αx␈↓ = d␈↓αf/␈↓d␈↓αx + ␈↓d␈↓αg␈↓/d␈↓αx.

␈↓ ↓H␈↓We would see:␈↓ ∧'␈↓αu = ␈↓
R␈↓∞(␈↓α f + g ␈↓∞)␈↓  = ␈↓α(PLUS, ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α, ␈↓
R␈↓∞( ␈↓αg ␈↓∞)␈↓α)␈↓
␈↓ ↓H␈↓Where:␈↓ αX␈↓αsecond[u] = ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓ and, ␈↓α
␈↓ ↓H␈↓α␈↓ αXthird[u] = ␈↓
R␈↓∞(␈↓α g ␈↓∞)␈↓.        ␈↓π 40␈↓


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓α␈↓π 39␈↓α␈α∂␈↓This␈α∂is␈α∂messier␈α∂than␈α∂it␈α∂really␈α∂needs␈α∂to␈α∂be␈α∂because␈α∂we␈α∂assume␈α∂that␈α∂+␈α∂and␈α∂*␈α∂are␈α∂binary.␈α∞You
␈↓ ↓H␈↓should␈α⊂also␈α⊂notice␈α⊂that␈α⊂our␈α⊂␈↓
R␈↓-mapping␈α⊂is␈α⊃applicable␈α⊂to␈α⊂a␈α⊂larger␈α⊂class␈α⊂of␈α⊂expressions␈α⊃than␈α⊂just
␈↓ ↓H␈↓<poly>. Look at ␈↓α(x + y)*(z + 2).

␈↓ ↓H␈↓␈↓π 40␈↓␈α∂As␈α∂we␈α∂intimated␈α∂earlier,␈α∂we␈α∂have␈α∂done␈α⊂an␈α∂evil␈α∂thing␈α∂here.␈α∂We␈α∂have␈α∂tied␈α∂the␈α⊂algorithm␈α∂for
␈↓ ↓H␈↓symbolic␈α∞differentiation␈α∞to␈α∞a␈α∞specific␈α∞representation␈α∞for␈α∞polynomials.␈α∞It␈α∞is␈α∞useful␈α∞to␈α∞use␈α∂a␈α∞specific
␈↓ ↓H␈↓representation for the moment and repent later. In particular, see page 62, page 70 and page .
␈↓ ↓H␈↓␈↓↓60  Applications␈↓ 53.3␈↓


␈↓ ↓H␈↓The result of differentiating ␈↓αu␈↓ is to be a new list of three elements:

␈↓ ↓H␈↓␈↓ αX1.  the symbol ␈↓αPLUS␈↓.
␈↓ ↓H␈↓␈↓ αX2.  the effect of ␈↓αdiff␈↓ operating ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓
␈↓ ↓H␈↓␈↓ αX3.  the effect of ␈↓αdiff␈↓ operating ␈↓
R␈↓∞( ␈↓αg ␈↓∞)␈↓

␈↓ ↓H␈↓Thus another part of the algorithm:

␈↓ ↓H␈↓α␈↓ αXeq [first[u];PLUS] →␈↓ ∧xlist [PLUS; diff[second[u];x];diff[third[u];x]].
␈↓ ↓H␈↓α␈↓.


␈↓ ↓H␈↓d[␈↓αf*g]␈↓/d␈↓αx␈↓ is defined to be ␈↓αf* ␈↓d␈↓αg␈↓/d␈↓αx + g *␈↓d␈↓αf/␈↓d␈↓αx␈↓.


␈↓ ↓H␈↓So here's another part of ␈↓αdiff␈↓:
␈↓ ↓H␈↓α␈↓ αXeq[first[u];TIMES] →␈↓ ∧xlist[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧x     list[TIMES; second[u];diff [third[u];x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧x     list[TIMES;third[u];diff [second[u];x]]]

␈↓ ↓H␈↓Finally, here's an example. We know:

␈↓ ↓H␈↓␈↓ ¬Kd[␈↓αx*y + x]␈↓/d␈↓αx = y + 1␈↓
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     61␈↓



␈↓ ↓H␈↓Try: ␈↓α
␈↓ ↓H␈↓αdiff [(PLUS (TIMES X Y) X); X]
␈↓ ↓H␈↓α␈↓ αX=list [PLUS; diff[(TIMES X Y); X]; diff [X;X]]
␈↓ ↓H␈↓α␈↓ αX=list [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ β(list [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ β(␈↓ βhlist [TIMES; X; diff [Y;X]];
␈↓ ↓H␈↓α␈↓ αX␈↓ β(␈↓ βhlist [TIMES; Y; diff [X;X]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ β(diff [X;X]]

␈↓ ↓H␈↓α␈↓ αX=list [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ β(list [PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ β(␈↓ βhlist [TIMES; X ;0];
␈↓ ↓H␈↓α␈↓ αX␈↓ β(␈↓ βhlist [TIMES; Y;1]];
␈↓ ↓H␈↓α␈↓ αX␈↓ β(1 ]

␈↓ ↓H␈↓α␈↓ αX=(PLUS  (PLUS (TIMES X 0) (TIMES Y 1)) 1)


␈↓ ↓H␈↓α␈↓which can be interpreted as:

␈↓ ↓H␈↓␈↓α␈↓ ¬qx*0 + y*1 + 1 . ␈↓ 

␈↓ ↓H␈↓Now␈α
it␈α
is␈α
clear␈α
that␈α
we␈α
have␈α
the␈α
right␈α
answer;␈α
it␈α
is␈α
equally␈α
clear␈α
that␈α
the␈α
representation␈αleaves␈α
much
␈↓ ↓H␈↓to␈α∞be␈α
desired.␈α∞There␈α
are␈α∞obvious␈α
simplifications␈α∞which␈α
we␈α∞would␈α
expect␈α∞to␈α
have␈α∞done␈α∞before␈α
we
␈↓ ↓H␈↓would␈α⊂consider␈α⊂this␈α⊂output␈α⊂acceptable.␈α⊂This␈α∂example␈α⊂is␈α⊂a␈α⊂particularly␈α⊂simple␈α⊂case␈α⊂for␈α∂algebraic
␈↓ ↓H␈↓simplification.␈α
We␈αcan␈α
easily␈αwrite␈α
a␈αLISP␈α
program␈αto␈α
perform␈αsimplifications␈α
like␈α
those␈αexpected
␈↓ ↓H␈↓here:␈α∂like␈α∂replacing␈α∂␈↓α0*x␈↓␈α∂by␈α∂␈↓α0␈↓,␈α∂and␈α∂␈↓αx*1␈↓␈α∂by␈α∂␈↓αx␈↓.␈α∂But␈α∂the␈α∂general␈α∂problem␈α∂of␈α∂writing␈α⊂simplifiers,␈α∂or
␈↓ ↓H␈↓indeed␈α⊂of␈α∂recognizing␈α⊂what␈α⊂is␈α∂a␈α⊂simplification,␈α⊂is␈α∂quite␈α⊂difficult.␈α⊂ A␈α∂whole␈α⊂branch␈α⊂of␈α∂computer
␈↓ ↓H␈↓science␈α⊂has␈α⊂grown␈α⊂up␈α⊃around␈α⊂symbolic␈α⊂and␈α⊂algebraic␈α⊃manipulation␈α⊂of␈α⊂expressions.␈α⊂One␈α⊃of␈α⊂the
␈↓ ↓H␈↓crucial parts of such an endeavor is a sophisticated simplifier.



␈↓ ↓H␈↓␈↓ ¬q␈↓↓Points to note␈↓


␈↓ ↓H␈↓This␈αproblem␈αof␈αrepresentation␈αis␈αtypical␈αof␈αdata␈αstructure␈αalgorithms␈α(regardless␈αof␈αwhat␈αlanguage
␈↓ ↓H␈↓you␈α⊂use).␈α∂ That␈α⊂is,␈α⊂once␈α∂you␈α⊂have␈α∂decided␈α⊂what␈α⊂the␈α∂intuitive␈α⊂problem␈α∂is,␈α⊂pick␈α⊂a␈α∂representation
␈↓ ↓H␈↓which␈αmakes␈αyour␈αalgorithms␈αclean␈α(only␈αlater␈αshould␈αyou␈αworry␈αabout␈αefficiency).␈α In␈αthe␈αnext␈αset
␈↓ ↓H␈↓of␈α
examples␈α
we␈α
will␈α
see␈α
a␈α
series␈α
of␈α
representations,␈α
each␈α
becoming␈α
more␈α
and␈α
more␈α
"efficient"␈αand
␈↓ ↓H␈↓each requiring more "knowledge" being built into the algorithm.
␈↓ ↓H␈↓␈↓↓62  Applications␈↓ 53.3␈↓



␈↓ ↓H␈↓Here's the whole algorithm for differentiation using + and *.  ␈↓α

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isindiv[u] → [eq [x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX eq [first [u]; PLUS] → list␈↓ ¬H[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H diff [second [u]; x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H diff [third [u]; x]];
␈↓ ↓H␈↓α␈↓ αX eq [first[u]; TIMES] → list␈↓ ¬H[PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H list␈↓ ελ[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H␈↓ ελ second[u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H␈↓ ελ diff [third [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H list␈↓ ελ[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H␈↓ ελ third [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬H␈↓ ελ diff [second[u]; x]]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → ␈↓λB␈↓α]

␈↓ ↓H␈↓As␈α⊂we␈α⊂mentioned␈α∂earlier,␈α⊂the␈α⊂current␈α⊂manifestation␈α∂of␈α⊂␈↓αdiff␈↓␈α⊂encodes␈α∂too␈α⊂much␈α⊂of␈α⊂our␈α∂particular
␈↓ ↓H␈↓representation␈αfor␈αpolynomials.␈αThe␈αseparation␈αof␈αalgorithm␈αfrom␈αrepresentation␈αis␈αbeneficial␈αfrom
␈↓ ↓H␈↓at␈α∩least␈α∩two␈α∩standpoints.␈α∩ First,␈α∩changing␈α⊃representation␈α∩should␈α∩have␈α∩a␈α∩minimal␈α∩effect␈α∩on␈α⊃the
␈↓ ↓H␈↓structure␈α∞of␈α∞the␈α∞algorithm,␈α
but␈α∞␈↓αdiff␈↓␈α∞␈↓↓knows␈↓␈α∞that␈α
variables␈α∞are␈α∞represented␈α∞as␈α
atoms␈α∞and␈α∞a␈α∞sum␈α
is
␈↓ ↓H␈↓represented as a list whose ␈↓αfirst␈↓-part is ␈↓αPLUS␈↓.  Second, readability of the algorithm suffers greatly.

␈↓ ↓H␈↓How␈α∂much␈α∞of␈α∂␈↓αdiff␈↓␈α∞really␈α∂needs␈α∞to␈α∂know␈α∞about␈α∂the␈α∞representation␈α∂and␈α∞how␈α∂can␈α∞we␈α∂improve␈α∞the
␈↓ ↓H␈↓readability of ␈↓αdiff␈↓?

␈↓ ↓H␈↓To␈αbegin␈αwith␈αthe␈α
uses␈αof␈α␈↓αfirst␈↓,␈α␈↓αsecond␈↓,␈α
and␈α␈↓αthird␈↓␈αare␈αnot␈α
particularly␈αmnemonic␈↓π 41␈↓.␈αWe␈αused␈α
␈↓αsecond␈↓
␈↓ ↓H␈↓to␈α
get␈αthe␈α
first␈α
argument␈αto␈α
a␈αsum␈α
or␈α
product␈αand␈α
used␈α␈↓αthird␈↓␈α
to␈α
get␈αthe␈α
second.␈α We␈α
used␈α
␈↓αfirst␈↓␈αto
␈↓ ↓H␈↓extract the operator.

␈↓ ↓H␈↓Let's define the selectors:
␈↓ ↓H␈↓α␈↓ ¬hop[x] <= first[x]
␈↓ ↓H␈↓α␈↓ ¬Narg␈↓β1␈↓α[x] <= second[x]
␈↓ ↓H␈↓α␈↓ ¬Warg␈↓β2␈↓α[x] <= third[x]










␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 41␈↓ It must be admitted, however, that they are more readable than ␈↓αcar-cdr␈↓-chains.
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     63␈↓



␈↓ ↓H␈↓Then ␈↓αdiff␈↓ becomes:

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isindiv[u] → [eq [x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX eq [op[u]; PLUS] → list␈↓ ¬([PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬( diff [arg␈↓β1␈↓α [u]; x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬( diff [arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX eq [op[u]; TIMES] → list␈↓ ¬([PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬( list␈↓ ¬h[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬(␈↓ ¬h arg␈↓β1␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬(␈↓ ¬h diff [arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬( list␈↓ ¬h[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬(␈↓ ¬h arg␈↓β2␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ¬(␈↓ ¬h diff [arg␈↓β1␈↓α [u]; x]]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → ␈↓λB␈↓α]

␈↓ ↓H␈↓Still,␈αthere␈αis␈αmuch␈αof␈αthe␈αrepresentation␈α
present.␈αRecognition␈αof␈αvariables␈αand␈αother␈αterms␈α
can␈αbe
␈↓ ↓H␈↓abstracted␈α∞from␈α∞the␈α∂representation.␈α∞We␈α∞need␈α∂only␈α∞recognize␈α∞when␈α∂a␈α∞term␈α∞is␈α∂a␈α∞sum,␈α∞a␈α∂product,␈α∞a
␈↓ ↓H␈↓variable␈α∞or␈α∂a␈α∞constant.␈α∞ To␈α∂test␈α∞for␈α∞the␈α∂occurrence␈α∞of␈α∞a␈α∂numeral␈α∞we␈α∞shall␈α∂assume␈α∞a␈α∂unary␈α∞LISP
␈↓ ↓H␈↓predicate␈αcalled␈α␈↓αnumberp␈↓␈αwhich␈αreturns␈α␈↓
t␈↓␈αjust␈αin␈αthe␈αcase␈αthat␈αits␈αargument␈αis␈αa␈αnumeral.␈α Then,␈αin
␈↓ ↓H␈↓terms of the current representation, we could define such recognizers or predicates as:

␈↓ ↓H␈↓α␈↓ ¬≠issum[x] <= eq[op[x];PLUS]
␈↓ ↓H␈↓α␈↓ ¬∞isprod[x] <= eq[op[x];TIMES]
␈↓ ↓H␈↓α␈↓ ¬2isconst[x] <= numberp[x]
␈↓ ↓H␈↓α␈↓ ∧*isvar[x] <= [isindiv[x] → not[isconst[x]]; ␈↓
t␈↓α → ␈↓
f␈↓α]
␈↓ ↓H␈↓α␈↓ ¬=samevar[x;y] <= eq[x;y]


␈↓ ↓H␈↓Using these predicates we can rewrite ␈↓αdiff␈↓ as:

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX isconst[u] → 0;
␈↓ ↓H␈↓α␈↓ αX issum[u] → list␈↓ ∧([PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( diff [arg␈↓β1␈↓α [u]; x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( diff [arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX isprod[u] → list␈↓ ∧([PLUS;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( list␈↓ ∧h[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧(␈↓ ∧h arg␈↓β1␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧(␈↓ ∧h diff [arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧( list␈↓ ∧h[TIMES;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧(␈↓ ∧h arg␈↓β2␈↓α [u];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧(␈↓ ∧h diff [arg␈↓β1␈↓α [u]; x]]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓ ↓H␈↓␈↓↓64  Applications␈↓ 53.3␈↓


␈↓ ↓H␈↓Readability␈α
is␈α
certainly␈α
improving,␈α
but␈α
the␈α
representation␈αis␈α
still␈α
known␈α
to␈α
␈↓αdiff␈↓.␈α
 When␈α
we␈αbuild␈α
the
␈↓ ↓H␈↓result␈α⊂of␈α∂the␈α⊂sum␈α∂or␈α⊂product␈α∂of␈α⊂derivatives␈α∂we␈α⊂use␈α∂knowledge␈α⊂of␈α∂the␈α⊂representation.␈α⊂ Rather␈α∂it
␈↓ ↓H␈↓would be better to define:

␈↓ ↓H␈↓α␈↓ ¬↓makesum[x;y] <= list[PLUS;x;y]
␈↓ ↓H␈↓α␈↓ ∧smakeprod[x;y] <= list[TIMES;x;y]

␈↓ ↓H␈↓Then the new ␈↓αdiff␈↓ is:

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX isconst[u] → 0;
␈↓ ↓H␈↓α␈↓ αX issum[u] → makesum[diff [arg␈↓β1␈↓α [u]; x];diff[arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX isprod[u] → makesum[␈↓ ∧xmakeprod[arg␈↓β1␈↓α [u];diff[arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧x makeprod[arg␈↓β2␈↓α [u];diff [arg␈↓β1␈↓α [u]; x]]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → ␈↓λB␈↓α]

␈↓ ↓H␈↓Notice␈α
that␈α
␈↓αdiff␈↓␈α
is␈α
much␈α
more␈α
readable␈αnow␈α
and,␈α
more␈α
importantly,␈α
the␈α
details␈α
of␈αthe␈α
representation
␈↓ ↓H␈↓have␈α
been␈α
relegated␈α
to␈α
subfunctions.␈α
Changing␈α
representation␈α
simply␈α
requires␈α∞supplying␈α
different
␈↓ ↓H␈↓subfunctions.␈αNo␈α
changes␈αneed␈α
be␈αmade␈α
to␈α␈↓αdiff␈↓.␈α
There␈αhas␈α
only␈αbeen␈α
a␈αslight␈α
decrease␈αin␈α
efficiency.
␈↓ ↓H␈↓The␈α⊃termination␈α⊃condition␈α⊂in␈α⊃the␈α⊃original␈α⊂␈↓αdiff␈↓␈α⊃is␈α⊃a␈α⊂bit␈α⊃more␈α⊃succinct.␈α⊂ Looking␈α⊃back,␈α⊃we␈α⊂first
␈↓ ↓H␈↓abstracted␈α↔the␈α↔selector␈α↔functions:␈α↔those␈α↔which␈α↔selected␈α↔components;␈α↔then␈α↔we␈α↔abstracted␈α⊗the
␈↓ ↓H␈↓recognizers:␈α∞the␈α
predicates␈α∞telling␈α
which␈α∞term␈α
was␈α∞present;␈α
then␈α∞we␈α
modified␈α∞the␈α∞constructors:␈α
the
␈↓ ↓H␈↓functions␈α
which␈αmake␈α
new␈αterms.␈α
 These␈αthree␈α
components␈αof␈α
programming:␈α
selectors,␈αrecognizers,
␈↓ ↓H␈↓and constructors, will appear again on page  in discussing McCarthy's abstract syntax.

␈↓ ↓H␈↓The␈α∂␈↓αdiff␈↓␈α∞algorithm␈α∂is␈α∞abstract␈α∂now,␈α∞in␈α∂the␈α∂sense␈α∞that␈α∂the␈α∞representation␈α∂of␈α∞the␈α∂domain␈α∂and␈α∞the
␈↓ ↓H␈↓representation␈αof␈αthe␈αfunctions␈αand␈αpredicates␈αwhich␈αmanipulate␈αthat␈αdomain␈αhave␈αbeen␈αextracted
␈↓ ↓H␈↓out.␈α∂This␈α∞is␈α∂our␈α∞␈↓λr␈↓-mapping␈α∂again;␈α∞we␈α∂mapped␈α∞the␈α∂domain␈α∞of␈α∂<poly>'s␈α∞to␈α∂lists␈α∞and␈α∂mapped␈α∞the
␈↓ ↓H␈↓constructors,␈α∞selectors,␈α∞and␈α∞recognizers␈α∞to␈α∞list-manipulating␈α
functions.␈α∞ Thus␈α∞the␈α∞data␈α∞types␈α∞of␈α
the
␈↓ ↓H␈↓arguments␈α
␈↓αu␈↓␈α
and␈α
␈↓αx␈↓␈α∞are␈α
<poly>␈α
and␈α
<var>␈α∞respectively,␈α
␈↓↓not␈↓␈α
list␈α
and␈α∞atom.␈α
To␈α
stress␈α
this␈α∞point␈α
we
␈↓ ↓H␈↓should␈αmake␈αone␈αmore␈αtransformation␈αon␈α␈↓αdiff␈↓.␈αWe␈αhave␈αfrequently␈αsaid␈αthat␈αthere␈αis␈αa␈αsubstantial
␈↓ ↓H␈↓parallel␈α∂between␈α∂a␈α⊂data␈α∂structure␈α∂and␈α⊂the␈α∂algorithms␈α∂which␈α⊂manipulate␈α∂it.␈α∂Paralleling␈α⊂the␈α∂BNF
␈↓ ↓H␈↓definition of <poly> on page 58, we write:

␈↓ ↓H␈↓αdiff[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isterm[u] → diffterm[u;x];
␈↓ ↓H␈↓α␈↓ αX issum[u] → makesum[diff [arg␈↓β1␈↓α [u]; x];diff[arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → ␈↓λB␈↓α]

␈↓ ↓H␈↓αdiffterm[u;x] <=
␈↓ ↓H␈↓α␈↓ αX[isconst[u] → 0;
␈↓ ↓H␈↓α␈↓ αX isvar[u] → [samevar[x;u] → 1; ␈↓
t␈↓α → 0];
␈↓ ↓H␈↓α␈↓ αX isprod[u] → makesum[␈↓ ∧xmakeprod[arg␈↓β1␈↓α [u];diff[arg␈↓β2␈↓α [u]; x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧x makeprod[arg␈↓β2␈↓α [u];diff [arg␈↓β1␈↓α [u]; x]]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → ␈↓λB␈↓α]
␈↓ ↓H␈↓␈↓↓3.3␈↓ 	9Differentiation     65␈↓


␈↓ ↓H␈↓To satisfy our complaint of page 58 that ␈↓αdiff[1; 1]␈↓ gives a defined result, we should also add:

␈↓ ↓H␈↓α␈↓ αXdiff␈↓λ'␈↓α[u; x] <= [␈↓ ∧_isvar[x] → [ispoly[u] → diff[u; x]]; ␈↓
t␈↓α → ␈↓λB␈↓α]

␈↓ ↓H␈↓Finally,␈α∀notice␈α∀that␈α∃our␈α∀abstraction␈α∀process␈α∀has␈α∃masked␈α∀the␈α∀order-dependence␈α∃of␈α∀conditional
␈↓ ↓H␈↓expressions. Exactly one of the recognizers will be satisfied by the form ␈↓αu␈↓.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓␈↓↓1.␈↓ Extend the version of ␈↓αdiff␈↓ of your choice to handle differentiation of powers such as ␈↓α↑[x; 3]␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓ Extend ␈↓αdiff␈↓ to handle unary minus.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α⊃Extend␈α⊃␈↓αdiff␈↓␈α⊃to␈α⊃handle␈α⊃differentiation␈α∩of␈α⊃the␈α⊃trignometric␈α⊃functions,␈α⊃␈↓αsin␈↓␈α⊃and␈α⊃␈↓αcons␈↓␈α∩and␈α⊃their
␈↓ ↓H␈↓composition with polynomials.  For example it should handle ␈↓αsin␈↓π2␈↓αx + cos(x␈↓π3␈↓α + 5x -2)␈↓.

␈↓ ↓H␈↓␈↓↓4.␈↓ Write an algorithm to handle integration of polynomials.



␈↓ ↓H␈↓␈↓ ¬j␈↓↓3.4  Data Bases␈↓


␈↓ ↓H␈↓*****DON'T READ YET; VERY INCOMPLETE*****

␈↓ ↓H␈↓One␈α⊂of␈α∂the␈α⊂more␈α∂intriguing␈α⊂applications␈α∂of␈α⊂LISP␈α∂is␈α⊂in␈α∂the␈α⊂area␈α∂of␈α⊂data␈α∂base␈α⊂manipulation.␈α∂In
␈↓ ↓H␈↓Section 6.3␈α
we␈α∞will␈α
discuss␈α
data␈α∞bases␈α
in␈α∞a␈α
more␈α
general␈α∞setting.␈α
 In␈α
this␈α∞section␈α
we␈α∞introduce␈α
the
␈↓ ↓H␈↓ideas suggest how LISP can be applied to the problems.

␈↓ ↓H␈↓A␈α
data␈α
base␈αis␈α
a␈α
collection␈αof␈α
objects␈α
together␈αwith␈α
a␈α
set␈αof␈α
functions␈α
to␈αpose␈α
questions␈α
about␈αthe
␈↓ ↓H␈↓objects␈α
in␈αthe␈α
base,␈α
to␈αselect␈α
objects␈α
from␈αthe␈α
base,␈αand␈α
to␈α
construct␈αnew␈α
entries␈α
in␈αthe␈α
base.␈αThat␈α
is,
␈↓ ↓H␈↓a␈α
data␈α
base␈α
is␈α
an␈α
abstract␈α
data␈α
structure.␈α We␈α
need␈α
to␈α
locate␈α
information␈α
in␈α
the␈α
base.␈α
 We␈αshould␈α
be
␈↓ ↓H␈↓able␈α∞to␈α∞ask␈α
the␈α∞system␈α∞for␈α∞a␈α
specific␈α∞object␈α∞or␈α
we␈α∞should␈α∞be␈α∞able␈α
to␈α∞partially␈α∞specify␈α∞our␈α
request
␈↓ ↓H␈↓("find␈α
all␈α
books␈α
about␈α
LISP"␈αor␈α
"find␈α
all␈α
books␈α
about␈αLISP␈α
published␈α
before␈α
1975").␈α
 We␈αshould␈α
be
␈↓ ↓H␈↓able to add entries and delete entries, but we will postpone these kinds of requests until later.

␈↓ ↓H␈↓The␈αrepresentational␈αdetails␈αof␈αobjects␈αwill␈αbe␈αsuppressed␈αas␈αusual,␈αand␈αwe␈αwill␈αconcentrate␈α
on␈αthe
␈↓ ↓H␈↓abstract␈α
properties.␈α
 In␈α
our␈α
first␈αexample,␈α
the␈α
objects␈α
in␈α
the␈αdata␈α
base␈α
will␈α
represent␈α
constants:␈αan
␈↓ ↓H␈↓object will have a name and a collection of properties and values.
␈↓ ↓H␈↓%266  Applications␈↓ _3.4%*



␈↓"␈↓ ↓H␈↓
        ⊂αααααααπααααα⊃
␈↓"␈↓ ↓H␈↓
        ~ prop1 ~ val1~
␈↓"␈↓ ↓H␈↓
        εαααααααβαααααλ
␈↓"␈↓ ↓H␈↓
        ~ prop2 ~ val2~
␈↓"␈↓ ↓H␈↓
             # # #
␈↓"␈↓ ↓H␈↓
        εαααααααβαααααλ
␈↓"␈↓ ↓H␈↓
        ~ propn ~ valn~
␈↓"␈↓ ↓H␈↓
        %ααααααα∀ααααα$



␈↓ ↓H␈↓␈↓ ¬)␈↓↓An object representation␈↓


␈↓ ↓H␈↓For␈α
example,␈α
an␈αdata␈α
base␈α
dealing␈α
with␈αbusiness␈α
supplies␈α
might␈αhave␈α
objects␈α
named␈α
boxes.␈αEach
␈↓ ↓H␈↓box has properites like size and contents.

␈↓ ↓H␈↓Not␈α∞all␈α∞objects␈α∞need␈α∞to␈α∞have␈α∞the␈α∞same␈α
number␈α∞of␈α∞properties.␈α∞For␈α∞example␈α∞in␈α∞a␈α∞data␈α∞base␈α
whose
␈↓ ↓H␈↓objects␈αare␈αbibliographic␈αreferences,␈αbooks␈αneed␈αnot␈αhave␈αpage␈αreferences,␈αwhereas␈αjournal␈αarticles
␈↓ ↓H␈↓do;␈α⊃and␈α⊂journal␈α⊃references␈α⊂don't␈α⊃include␈α⊂a␈α⊃publisher␈α⊂whereas␈α⊃books␈α⊂do.␈α⊃ The␈α⊃programs␈α⊂which
␈↓ ↓H␈↓manipulate the data base must be structured to take changeablility into account.

␈↓ ↓H␈↓Here␈α∂are␈α⊂some␈α∂examples:␈α∂the␈α⊂first␈α∂one␈α∂was␈α⊂extracted␈α∂from␈α∂the␈α⊂side␈α∂of␈α∂a␈α⊂Xerox␈α∂paper␈α⊂box;␈α∂the
␈↓ ↓H␈↓second, is a representation of a bibliographic entry for this book.

␈↓"␈↓ ↓H␈↓
        ⊂αααααααπαααααααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~ NAME  ~  4029258   ~
␈↓"␈↓ ↓H␈↓
        εαααααααβααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~ SIZE  ~ 8-1/2 x 11 ~
␈↓"␈↓ ↓H␈↓
        εαααααααβααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~ COLOR ~   WHITE    ~
␈↓"␈↓ ↓H␈↓
        εαααααααβααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~ AMNT  ~  10 REAMS  ~
␈↓"␈↓ ↓H␈↓
        %ααααααα∀αααααααααααα$

␈↓"␈↓ ↓H␈↓
        ⊂ααααααααπααααααααααααααααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~ AUTHOR ~     ALLEN, JOHN, R. ~
␈↓"␈↓ ↓H␈↓
        εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~ TITLE  ~ THE ANATOMY OF LISP ~
␈↓"␈↓ ↓H␈↓
        εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~ TYPE   ~          BOOK       ~
␈↓"␈↓ ↓H␈↓
        εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~ PUBL   ~     MCGRAW-HILL     ~
␈↓"␈↓ ↓H␈↓
        εααααααααβαααααααααααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~ DATE   ~        1977         ~
␈↓"␈↓ ↓H␈↓
        %αααααααα∀ααααααααααααααααααααα$

␈↓ ↓H␈↓Given␈αa␈αdata␈αbase␈αof␈αobjects,␈αwe␈αneed␈αto␈αbe␈αable␈αto␈αmanipulate␈αthem␈αin␈αmeaningful␈αways.␈αWe␈αwill
␈↓ ↓H␈↓not␈αaddress␈αthe␈αproblems␈αof␈αdesigning␈αinput␈αand␈αoutput,␈αbut␈αwill␈αconcern␈αourselves␈αsolely␈αwith␈αthe
␈↓ ↓H␈↓problems of semantics data base primitives: how can we use the information in the base?
␈↓ ↓H␈↓␈↓↓3.4␈↓ 	wData Bases     67␈↓


␈↓ ↓H␈↓The␈αdescription␈αof␈α
functions␈αto␈αinspect,␈α
add,␈αor␈αdelete␈α
objects␈αfrom␈αthe␈α
data␈αbase␈αrequires␈αa␈α
pattern
␈↓ ↓H␈↓matcher. A pattern is

␈↓ ↓H␈↓do variables
␈↓ ↓H␈↓αmatch[pat;exp;mlist] <=␈↓ ∧_[eq[mlist;NO] → NO;
␈↓ ↓H␈↓α␈↓ ∧_ isconst[pat] → [same[pat;exp] → ␈↓
t␈↓α; ␈↓
t␈↓α → NO];
␈↓ ↓H␈↓α␈↓ ∧_ isvar[pat] → check[pat;exp;lookup[pat;mlist];mlist];
␈↓ ↓H␈↓α␈↓ ∧_ ␈↓
t␈↓α → match[␈↓ ¬(prefix[pat];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ¬(prefix[exp];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ¬(match[suffix[pat];suffix[pat];mlist]]
␈↓ ↓H␈↓αcheck[var;exp;val;mlist] <=␈↓ ¬λ[null[val] → concat[mkent[var;exp];mlist];
␈↓ ↓H␈↓α␈↓ ¬λ equal[exp;val] → mlist;
␈↓ ↓H␈↓α␈↓ ¬λ ␈↓
t␈↓α → NO]

␈↓ ↓H␈↓EXTENSIONS:␈α∩all␈α∩object␈α∩are␈α∩constants␈α∩so␈α∩far␈α∩allow␈α∩procedures␈α∩how␈α∩to␈α∩use␈α∪pdi???␈α∩ contexts,
␈↓ ↓H␈↓conniver and planner



␈↓ ↓H␈↓␈↓ ¬∀␈↓↓3.5  Algebra of polynomials␈↓


␈↓ ↓H␈↓Assume␈α⊂we␈α⊂want␈α⊂to␈α⊂perform␈α⊂addition␈α⊂and␈α⊂multiplication␈α⊂of␈α⊂polynomials␈α⊂and␈α⊂assume␈α⊃that␈α⊂each
␈↓ ↓H␈↓polynomial␈α∞is␈α∂of␈α∞the␈α∂form␈α∞␈↓αp␈↓β1␈↓α␈α∂+␈α∞p␈↓β2␈↓α␈α∂+␈α∞...␈α∂+␈α∞p␈↓βn␈↓␈α∂where␈α∞each␈α∂term,␈α∞␈↓αp␈↓βi␈↓,␈α∂is␈α∞a␈α∂product␈α∞of␈α∂variables␈α∞and
␈↓ ↓H␈↓constants.␈α∂ The␈α∂two␈α∞components␈α∂of␈α∂each␈α∂term␈α∞are␈α∂a␈α∂constant␈α∂part␈α∞called␈α∂the␈α∂coefficient,␈α∂and␈α∞the
␈↓ ↓H␈↓variable␈αpart.␈α We␈αshall␈α
assume␈αwithout␈αloss␈αof␈α
generality␈αthat␈αthe␈αset␈α
of␈αvariables␈αwhich␈αappear␈α
in
␈↓ ↓H␈↓the␈αpolynomials␈αare␈αlexicographically␈αordered,␈αe.g. ␈↓αx < y < z, ...,␈α␈↓and␈αassume␈αthat␈αeach␈αvariable␈αpart
␈↓ ↓H␈↓obeys␈α∞that␈α
ordering;␈α∞thus␈α∞we␈α
would␈α∞insist␈α∞that␈α
␈↓αxzy␈↓π2␈↓␈α∞be␈α∞written␈α
␈↓αxy␈↓π2␈↓αz␈↓.␈α∞ We␈α∞do␈α
not␈α∞assume␈α∞that␈α
the
␈↓ ↓H␈↓terms␈α
are␈α
ordered␈α
within␈αthe␈α
polynomial;␈α
thus␈α
␈↓αx + xy␈↓␈α
and␈α␈↓αxy + x␈↓␈α
are␈α
both␈α
acceptable.␈α
 We␈αfurther
␈↓ ↓H␈↓assume␈αthat␈αthe␈α
variables␈αof␈αeach␈α
␈↓αp␈↓βi␈↓␈αbe␈αdistinct␈α
and␈αthat␈αno␈α
␈↓αp␈↓βi␈↓␈αhave␈α␈↓α0␈↓␈α
as␈αcoefficient.␈α The␈α
standard
␈↓ ↓H␈↓algorithm␈α
for␈α
addition␈α
of␈α∞␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓␈α
with␈α
␈↓λS␈↓πm␈↓βj=1␈↓αq␈↓βj␈↓␈α
says␈α∞you␈α
can␈α
combine␈α
a␈α
␈↓αq␈↓βj␈↓␈α∞with␈α
a␈α
␈↓αp␈↓βi␈↓␈α
if␈α∞the␈α
variable
␈↓ ↓H␈↓parts␈α
of␈α
these␈α
terms␈α
are␈α
identical.␈α
 In␈α
this␈α
case␈α
the␈α
resulting␈α
term␈α
has␈α
the␈α
same␈α
variable␈α
part␈α
but␈α
has
␈↓ ↓H␈↓a␈αcoefficient␈αequal␈αto␈αthe␈αsum␈αof␈αthe␈αcoefficients␈αof␈α␈↓αp␈↓βi␈↓␈αand␈α␈↓αq␈↓βj␈↓.␈α For␈αexample␈αif␈α␈↓αp␈↓βi␈↓␈αis␈α␈↓α2x␈↓␈αand␈α␈↓αq␈↓βj␈↓␈αis␈α␈↓α3x␈↓
␈↓ ↓H␈↓the␈α∞sum␈α∞term␈α
is␈α∞␈↓α5x␈↓.␈α∞ We␈α∞will␈α
examine␈α∞four␈α∞representations␈α
of␈α∞polynomials,␈α∞before␈α∞finally␈α
writing
␈↓ ↓H␈↓any␈α
algorithms.␈α
To␈α∞aid␈α
in␈α
the␈α
discussion␈α∞we␈α
will␈α
use␈α
the␈α∞polynomial␈α
␈↓αx␈↓π2␈↓α␈α
-␈α
2y␈α∞-␈α
z␈↓␈α
as␈α∞our␈α
canonical
␈↓ ↓H␈↓example.



␈↓ ↓H␈↓␈↓ ¬H␈↓↓First representation:␈↓


␈↓ ↓H␈↓We␈αcould␈α
use␈αthe␈αrepresentation␈α
of␈αthe␈α
differentiation␈αexample.␈α This␈α
would␈αrepresent␈αour␈α
example
␈↓ ↓H␈↓as:
␈↓ ↓H␈↓α␈↓ α|(PLUS (TIMES 1(EXPT X 2)) (PLUS (TIMES -2 Y) (TIMES -1 Z)))
␈↓ ↓H␈↓␈↓↓68  Applications␈↓ 43.5␈↓


␈↓ ↓H␈↓The␈α
above␈αconventions␈α
specify␈α
an␈αunambiguous␈α
representation␈α
for␈αour␈α
class␈α
of␈αpolynomials.␈α
Strictly
␈↓ ↓H␈↓speaking␈α∂we␈α∞did␈α∂not␈α∞need␈α∂to␈α∂impose␈α∞the␈α∂ordering␈α∞on␈α∂the␈α∂set␈α∞of␈α∂variables.␈α∞However,␈α∂we␈α∂need␈α∞to
␈↓ ↓H␈↓impose␈α
some␈αadditional␈α
constraints␈α
before␈αwe␈α
have␈α
data␈αstructures␈α
which␈α
are␈αwell-suited␈α
to␈αthe␈α
class
␈↓ ↓H␈↓of polynomial algorithms we wish to represent.



␈↓ ↓H␈↓␈↓ ¬9␈↓↓Second representation:␈↓


␈↓ ↓H␈↓We␈α∀are␈α∀really␈α∀only␈α∀interested␈α∀in␈α∀testing␈α∀the␈α∀equality␈α∀of␈α∀the␈α∀variable␈α∀parts;␈α∀we␈α∀will␈α∃not␈α∀be
␈↓ ↓H␈↓manipulating␈αthem␈α
in␈αany␈α
other␈αway.␈α So␈α
we␈αmight␈α
simply␈αrepresent␈αthe␈α
variable␈αpart␈α
as␈αa␈α
list␈αof
␈↓ ↓H␈↓pairs;␈αeach␈α
pair␈αcontains␈αa␈α
variable␈αname␈α
and␈αthe␈αcorresponding␈α
value␈αof␈α
the␈αexponent.␈α Using␈α
our
␈↓ ↓H␈↓knowledge␈αof␈αthe␈αforms␈αof␈αpolynomials␈αand␈αthe␈αclass␈αof␈αalgorithms␈αwe␈αwish␈αto␈αimplement,␈αwe␈αwrite
␈↓ ↓H␈↓␈↓λS ␈↓αp␈↓βi␈↓ as:
␈↓ ↓H␈↓␈↓ ¬~␈↓α( (␈↓rep of ␈↓αp␈↓β1␈↓α), (␈↓rep of ␈↓αp␈↓β2␈↓α), ...)␈↓

␈↓ ↓H␈↓which would make our example look like:

␈↓ ↓H␈↓α␈↓ β#((TIMES 1 ((X . 2))), (TIMES -2 ((Y . 1))), (TIMES -1 ((Z . 1))))

␈↓ ↓H␈↓Is␈αthis␈αrepresentation␈αsufficient?␈α Does␈αit␈αhave␈αthe␈αflexibility␈αwe␈αneed?␈α It␈α␈↓↓does␈↓␈αsuffice␈αbut␈αit␈αis␈αstill
␈↓ ↓H␈↓not terribly efficient. We are ignoring too much of the structure in our class of polynomials.



␈↓ ↓H␈↓␈↓ ¬A␈↓↓Third representation:␈↓


␈↓ ↓H␈↓What␈αdo␈αwe␈αknow?␈αWe␈αknow␈αthat␈αthe␈αoccurrence␈αof␈αvariables␈αis␈αordered␈αin␈αeach␈αvariable␈αpart;␈αwe
␈↓ ↓H␈↓can␈α
assume␈αthat␈α
we␈αknow␈α
the␈αclass␈α
of␈αvariables␈α
which␈αmay␈α
appear␈αin␈α
any␈αpolynomial.␈α
So␈αinstead␈α
of
␈↓ ↓H␈↓writing ␈↓αx␈↓π2␈↓αy␈↓π3␈↓αz␈↓ as
␈↓ ↓H␈↓␈↓ ∧T␈↓α((X . 2) (Y . 3) (Z . 1))␈↓, we could write:


␈↓ ↓H␈↓␈↓ ∧≠␈↓α(2 3 1)␈↓   assuming ␈↓αx, y, z␈↓ are the only variables.

␈↓ ↓H␈↓In␈αa␈αfurther␈αsimplification,␈αnotice␈αthat␈αthe␈α␈↓αTIMES␈↓␈αin␈αthe␈αrepresentation␈αis␈αsuperfluous.␈αWe␈α␈↓↓always␈↓
␈↓ ↓H␈↓multiply␈αthe␈αcoefficient␈αby␈αthe␈αvariable␈αpart.␈αSo␈αwe␈αcould␈αsimply␈α␈↓αcons␈↓␈αthe␈αcoefficient␈αonto␈αthe␈αfront
␈↓ ↓H␈↓of the variable part representation.
␈↓ ↓H␈↓␈↓↓3.5␈↓ λIAlgebra of polynomials     69␈↓


␈↓ ↓H␈↓Let's stop for some examples.

␈↓ ↓H␈↓␈↓ ¬_␈↓↓term␈↓ πλrepresentation
␈↓ ↓H␈↓↓␈↓ ¬_␈↓α2xyz␈↓ πλ(2 1 1 1)
␈↓ ↓H␈↓α␈↓ ¬_2x␈↓π2␈↓αz␈↓ πλ(2 2 0 1)
␈↓ ↓H␈↓α␈↓ ¬_4z␈↓π3␈↓α␈↓ πλ(4 0 0 3)

␈↓ ↓H␈↓Thus our canonical polynomial would now be represented as:
␈↓ ↓H␈↓α␈↓ ¬λ((1 2 0 0) (-2 0 1 0) (-1 0 0 1))

␈↓ ↓H␈↓This␈αrepresentation␈αis␈αnot␈αtoo␈αbad;␈αthe␈α␈↓αfirst␈↓-part␈α
of␈αany␈αterm␈αis␈αthe␈αcoefficient;␈αthe␈α␈↓αrest␈↓-part␈α
is␈αthe
␈↓ ↓H␈↓variable part. So, for example the test for equality of variable parts is simply a call on ␈↓αequal␈↓.

␈↓ ↓H␈↓Now let's start thinking about the structure of the main algorithm.



␈↓ ↓H␈↓␈↓ ¬;␈↓↓Fourth representation:␈↓


␈↓ ↓H␈↓The␈αalgorithm␈αfor␈αthe␈αsum␈αmust␈αcompare␈αterms;␈αfinding␈αlike␈αterms␈αit␈αwill␈αgenerate␈αan␈αappropriate
␈↓ ↓H␈↓new␈αterm,␈α
otherwise␈αsimply␈αcopy␈α
the␈αterms.␈α When␈α
we␈αpick␈αa␈α
␈↓αp␈↓βi␈↓␈αfrom␈αthe␈α
first␈αpolynomial␈αwe␈α
would
␈↓ ↓H␈↓like␈αto␈αfind␈αa␈αcorresponding␈α␈↓αq␈↓βj␈↓␈αwith␈αthe␈αminimum␈αamount␈αof␈αsearching.␈α This␈αcan␈αbe␈αaccomplished
␈↓ ↓H␈↓if␈αwe␈αcan␈αorder␈α
the␈αterms␈αin␈αthe␈α
polynomials.␈αA␈αnatural␈αordering␈αcan␈α
be␈αinduced␈αon␈αthe␈α
terms␈αby
␈↓ ↓H␈↓ordering␈α⊃the␈α⊂numerical␈α⊃representation␈α⊂of␈α⊃the␈α⊂exponents.␈α⊃ For␈α⊂sake␈α⊃of␈α⊂argument,␈α⊃assume␈α⊃that␈α⊂a
␈↓ ↓H␈↓maximum␈α∂of␈α∂two␈α⊂digits␈α∂will␈α∂be␈α⊂needed␈α∂to␈α∂express␈α⊂the␈α∂exponent␈α∂of␈α⊂any␈α∂one␈α∂variable.␈α⊂Thus␈α∂the
␈↓ ↓H␈↓exponent␈α∩of␈α∩␈↓αx␈↓π2␈↓␈α⊃will␈α∩be␈α∩represented␈α⊃as␈α∩␈↓α02␈↓,␈α∩or␈α∩the␈α⊃exponent␈α∩of␈α∩␈↓αz␈↓π10␈↓␈α⊃will␈α∩be␈α∩represented␈α∩as␈α⊃␈↓α10␈↓.
␈↓ ↓H␈↓Combining this with our ordered representation of variable parts, we arrive at:

␈↓ ↓H␈↓␈↓ ¬_␈↓↓term␈↓ πλrepresentation

␈↓ ↓H␈↓α␈↓ ¬_43x␈↓π2␈↓αy␈↓π3␈↓αz␈↓π4␈↓ πλ␈↓α(43, 020304)
␈↓ ↓H␈↓α␈↓ ¬_2x␈↓π2␈↓αz␈↓ πλ(2, 020001)
␈↓ ↓H␈↓α␈↓ ¬_4z␈↓π3␈↓α␈↓ πλ(4, 000003)

␈↓ ↓H␈↓Now␈α∂we␈α⊂can␈α∂order␈α∂on␈α⊂the␈α∂numeric␈α⊂representation␈α∂of␈α∂the␈α⊂variable␈α∂part␈α∂of␈α⊂the␈α∂term.␈α⊂ One␈α∂more
␈↓ ↓H␈↓change of representation, which will result in a simplification in storage requirements:
␈↓ ↓H␈↓␈↓ ¬∧represent ␈↓α ax␈↓πA␈↓αy␈↓πB␈↓αz␈↓πC␈↓ as ␈↓α(a . ABC).

␈↓ ↓H␈↓This gives our final representation.
␈↓ ↓H␈↓α␈↓ ¬∞((1 . 20000) (-2 . 100) (-1 . 1))

␈↓ ↓H␈↓Note that ␈↓α20000 > 100 > 1␈↓.

␈↓ ↓H␈↓Finally␈αwe␈αwill␈αwrite␈αthe␈αalgorithm.␈α We␈αwill␈αassume␈αthat␈αthe␈αpolynomials␈αare␈αinitially␈αordered␈αand
␈↓ ↓H␈↓␈↓↓70  Applications␈↓ 43.5␈↓


␈↓ ↓H␈↓will␈α
write␈α
the␈α
algorithm␈α
so␈α
as␈α
to␈α
maintain␈αthat␈α
ordering.␈α
 Each␈α
term␈α
is␈α
a␈α
dotted␈α
pair␈α
of␈αelements:␈α
the
␈↓ ↓H␈↓coefficient and a representation of the variable part.

␈↓ ↓H␈↓As␈α∞in␈α
the␈α∞previous␈α∞differentiation␈α
example,␈α∞we␈α∞should␈α
attempt␈α∞to␈α∞extract␈α
the␈α∞algorithm␈α∞from␈α
the
␈↓ ↓H␈↓representation.

␈↓ ↓H␈↓We shall define:
␈↓ ↓H␈↓␈↓ ∧P␈↓αcoef[x] <= car[x]␈↓ and ␈↓α expo[x] <= cdr[x].

␈↓ ↓H␈↓To test the ordering we will use the LISP predicate:
␈↓ ↓H␈↓␈↓ ¬R␈↓αgreaterp[x;y] = x>y.

␈↓ ↓H␈↓In␈α
the␈α
construction␈α
of␈α
the␈α
`sum'␈α
polynomial␈α
we␈α
will␈α
generate␈α
new␈α
terms␈α
by␈α
combining␈αcoefficients.
␈↓ ↓H␈↓So a constructor named ␈↓αnode␈↓ is needed.  In terms of the latest representation ␈↓αnode␈↓ is defined as:
␈↓ ↓H␈↓␈↓ ¬C␈↓αnode[x;y] <= cons[x;y].␈↓

␈↓ ↓H␈↓So here's a graphical representation of our favorite polynomial:
␈↓ ↓H␈↓␈↓ ε
␈↓αx␈↓π2␈↓α - 2y - z ␈↓
␈↓"␈↓ ↓H␈↓∂                            /\
␈↓"␈↓ ↓H␈↓∂                           /  \
␈↓"␈↓ ↓H␈↓∂                          /    \
␈↓"␈↓ ↓H␈↓∂                         /      \
␈↓"␈↓ ↓H␈↓∂                        /\       \
␈↓"␈↓ ↓H␈↓∂                       /  \      /\
␈↓"␈↓ ↓H␈↓∂                      1  20000  /  \
␈↓"␈↓ ↓H␈↓∂                               /    \
␈↓"␈↓ ↓H␈↓∂                              /\     \
␈↓"␈↓ ↓H␈↓∂                             /  \     \
␈↓"␈↓ ↓H␈↓∂                           -2   100   /\
␈↓"␈↓ ↓H␈↓∂                                     /  \
␈↓"␈↓ ↓H␈↓∂                                    /   ␈↓αNIL␈↓∂
␈↓"␈↓ ↓H␈↓∂                                   /\
␈↓"␈↓ ↓H␈↓∂                                  /  \
␈↓"␈↓ ↓H␈↓∂                                 -1   1
␈↓ ↓H␈↓Here's the algorithm:
␈↓ ↓H␈↓αpolyadd[p;q] <=
␈↓ ↓H␈↓α␈↓ ↓h[null[p] →q; null[q] → p;
␈↓ ↓H␈↓α␈↓ ↓h eq[expo[first[p]];expo[first[q]]] →␈↓ ¬h[zerop[plus[coef[first[p]];coef[first[q]]]]
␈↓ ↓H␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h    → polyadd[rest[p];rest[q]];
␈↓ ↓H␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h ␈↓
t␈↓α → concat[node[plus[coef[first[p]];coef[first[q]]];
␈↓ ↓H␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h                      expo[first[p]]];
␈↓ ↓H␈↓α␈↓ ↓h␈↓ αX␈↓ ¬h                 polyadd[rest[p];rest[q]]]];
␈↓ ↓H␈↓α␈↓ ↓h greaterp[expo[first[p]];expo[first[q]]] → concat[first[p];polyadd[rest[p];q]];
␈↓ ↓H␈↓α␈↓ ↓h ␈↓
t␈↓α → concat[first[q];polyadd[p;rest[q]]]]
␈↓ ↓H␈↓␈↓↓3.5␈↓ λIAlgebra of polynomials     71␈↓


␈↓ ↓H␈↓Now for an explanation and example.

␈↓ ↓H␈↓First we used some new LISP functions:

␈↓ ↓H␈↓␈↓ ¬f␈↓αplus[x;y] = x + y
␈↓ ↓H␈↓α␈↓ ¬Rzerop[x]  <= eq[x;0]

␈↓ ↓H␈↓␈↓αpolyadd␈↓ is of the form: ␈↓α[p␈↓β1␈↓α → e␈↓β1␈↓α; p␈↓β2␈↓α → e␈↓β2␈↓α; p␈↓β3␈↓α → e␈↓β3␈↓α; p␈↓β4␈↓α → e␈↓β4␈↓α; p␈↓β5␈↓α → e␈↓β5␈↓α]


␈↓ ↓H␈↓Case i: ␈↓αp␈↓β1␈↓α → e␈↓β1␈↓ and ␈↓αp␈↓β2␈↓α → e␈↓β2␈↓. If either polynomial is empty return the other.

␈↓ ↓H␈↓Case␈α⊃ii:␈α⊃␈↓αp␈↓β3␈↓α␈α⊃→␈α⊃e␈↓β3␈↓.␈α⊃ If␈α⊃the␈α⊃variable␈α∩parts␈α⊃are␈α⊃equal␈α⊃then␈α⊃we␈α⊃can␈α⊃think␈α⊃about␈α∩combining␈α⊃terms.
␈↓ ↓H␈↓␈↓ αhHowever,␈α⊃we␈α⊃must␈α⊃check␈α⊃for␈α⊃cancellations␈α∩and␈α⊃not␈α⊃include␈α⊃any␈α⊃such␈α⊃terms␈α∩in␈α⊃our
␈↓ ↓H␈↓␈↓ αhresultant polynomial.

␈↓ ↓H␈↓Case␈α
iii:␈α
␈↓αp␈↓β4␈↓α␈α
→␈α
e␈↓β4␈↓␈α
and␈α
␈↓αp␈↓β5␈↓α␈α
→␈α
e␈↓β5␈↓.␈αThese␈α
sections␈α
worry␈α
about␈α
the␈α
ordering␈α
of␈α
terms␈α
so␈α
that␈αthe␈α
resultant
␈↓ ↓H␈↓␈↓ αhpolynomial retains the ordering.

␈↓ ↓H␈↓Here's an informal execution of ␈↓αpolyadd:

␈↓ ↓H␈↓αpolyadd[x+y+z;x␈↓π2␈↓α-2y-z]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;polyadd[x+y+z;-2y-z]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;polyadd[y+z;-2y-z]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[node[1+-2;y];polyadd[z;-z]]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[-y;polyadd[z;-z]]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[-y;polyadd[( );( )]]]]
␈↓ ↓H␈↓α␈↓ αX= concat[x␈↓π2␈↓α;concat[x;concat[-y;( )]]]
␈↓ ↓H␈↓α␈↓ ε≠= x␈↓π2␈↓α+x-y



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓␈↓↓1.␈↓ Write an algorithm, ␈↓αpolymult␈↓, to perform the the multiplication of two polynomials.



␈↓ ↓H␈↓␈↓ ¬↓␈↓↓3.6  Evaluation of polynomials␈↓


␈↓ ↓H␈↓Though␈α⊂you␈α⊂are␈α⊂undoubtedly␈α⊂quite␈α⊂tired␈α⊂of␈α⊂looking␈α⊂at␈α⊂polynomials,␈α⊂there␈α⊂is␈α⊂at␈α⊂least␈α⊂one␈α⊂more
␈↓ ↓H␈↓operation␈αwhich␈αis␈αusefully␈αperformed␈αon␈αsuch␈αcreatures.␈α That␈αoperation␈αis␈αevaluation.␈α Given␈αan
␈↓ ↓H␈↓arbitrary␈α⊂polynomial,␈α⊃and␈α⊂values␈α⊃for␈α⊂any␈α⊃of␈α⊂the␈α⊃variables␈α⊂which␈α⊃it␈α⊂contains,␈α⊃we␈α⊂would␈α⊃like␈α⊂to
␈↓ ↓H␈↓␈↓↓72  Applications␈↓ 53.6␈↓


␈↓ ↓H␈↓compute␈α
its␈α∞value.␈α
First␈α∞we␈α
will␈α∞assume␈α
that␈α∞the␈α
substitutions␈α∞of␈α
values␈α∞for␈α
variables␈α∞has␈α
already
␈↓ ↓H␈↓been␈αcarried␈αout.␈αThus␈αwe␈αare␈αdealing␈αwith␈αpolynomials␈αof␈αthe␈αform:␈α␈↓λS␈↓πn␈↓βi=1␈↓αp␈↓βi␈↓␈αwhere␈α␈↓αp␈↓βi␈↓␈αis␈αa␈αproduct
␈↓ ↓H␈↓of powers of constants. For example:

␈↓ ↓H␈↓␈↓ ¬v␈↓α2␈↓π3␈↓α + 3*4␈↓π2␈↓α + 5␈↓. 

␈↓ ↓H␈↓This could be represented as:

␈↓ ↓H␈↓␈↓ βU␈↓α(PLUS (EXPT 2 3) (PLUS (TIMES 3 (EXPT 4 2)) 5)).␈↓ 

␈↓ ↓H␈↓We␈αhave␈αtaken␈αthis␈α
general␈αrepresentation␈αbecause␈αwe␈α
have␈αgreat␈αexpectations␈αof␈α
generalizing␈αthe
␈↓ ↓H␈↓resulting algorithm.

␈↓ ↓H␈↓We␈α
will␈α
now␈α
describe␈α
a␈α
LISP␈α
function,␈α∞␈↓αvalue␈↓,␈α
which␈α
will␈α
take␈α
such␈α
an␈α
S-expr␈α∞representation␈α
and
␈↓ ↓H␈↓compute␈αits␈αvalue.␈α
Input␈αto␈α␈↓αvalue␈↓␈α
will␈αbe␈αnumerals␈α
or␈αlists␈αbeginning␈α
with␈αeither␈α␈↓αPLUS,␈αTIMES,␈↓␈α
or
␈↓ ↓H␈↓␈↓αEXPT␈↓.␈α
 The␈αvalue␈α
of␈αa␈α
numeral␈αis␈α
that␈αnumeral;␈α
to␈αevaluate␈α
the␈αother␈α
forms␈αof␈α
input␈α
we␈αshould
␈↓ ↓H␈↓perform␈α⊗the␈α⊗operation␈α⊗represented.␈α⊗We␈α⊗must␈α⊗therefore␈α⊗assume␈α⊗that␈α⊗operations␈α⊗of␈α∃addition,
␈↓ ↓H␈↓multiplication,␈α∞and␈α∞exponentiation␈α∞exist.␈α∞ Assume␈α∞they␈α∞are␈α∞named␈α∞+,␈α∞*,␈α∞and␈α∞↑,␈α∂respectively.␈α∞What
␈↓ ↓H␈↓then␈αshould␈αbe␈α
the␈αvalue␈αof␈αa␈α
representation␈αof␈αa␈αsum?␈α
 It␈αshould␈αbe␈αthe␈α
result␈αof␈αadding␈αthe␈α
value
␈↓ ↓H␈↓of␈αthe␈αrepresentations␈α
of␈αthe␈αtwo␈α
summands␈αor␈αoperands.␈α
That␈αis,␈α␈↓αvalue␈↓␈α
is␈αrecursive.␈α It␈αshould␈α
now
␈↓ ↓H␈↓be clear how to write ␈↓αvalue␈↓:

␈↓ ↓H␈↓αvalue[x] <=␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX issum[x] → +[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓ ↓H␈↓α␈↓ αX isprod[x] → *[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]];
␈↓ ↓H␈↓α␈↓ αX isexpt[x] → ↑[value[arg␈↓β1␈↓α[x]];value[arg␈↓β2␈↓α[x]]]]


␈↓ ↓H␈↓where:
␈↓ ↓H␈↓α␈↓ ¬≥isconstant[x] <= numberp[x]
␈↓ ↓H␈↓α␈↓ ¬∂issum[x] <= eq[first[x];PLUS]
␈↓ ↓H␈↓α␈↓ ¬↓isprod[x] <= eq[first[x];TIMES]
␈↓ ↓H␈↓α␈↓ ¬
isexpt[x] <= eq[first[x];EXPT]



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓1.  Show how to extend ␈↓α value␈↓ to handle binary and unary minus.

␈↓ ↓H␈↓2.␈α∪ Write␈α∩an␈α∪algorithm␈α∩␈↓αinstantiate␈↓␈α∪which␈α∪will␈α∩take␈α∪two␈α∩arguments,␈α∪one␈α∩representing␈α∪a␈α∪set␈α∩of
␈↓ ↓H␈↓variables␈α↔and␈α↔values,␈α↔the␈α↔other␈α↔representing␈α⊗a␈α↔polynomial.␈α↔The␈α↔algorithm␈α↔is␈α↔to␈α↔return␈α⊗a
␈↓ ↓H␈↓representation of the polynomial which would result from substituting the values for the variables.
␈↓ ↓H␈↓␈↓↓3.6␈↓ λ"Evaluation of polynomials     73␈↓


␈↓ ↓H␈↓3.␈α⊂ It␈α⊂would␈α∂be␈α⊂nice␈α⊂if␈α∂we␈α⊂could␈α⊂represent␈α∂expressions␈α⊂like␈α⊂2+3+4␈α∂as␈α⊂␈↓α(PLUS 2 3 4)␈↓␈α⊂rather␈α∂than
␈↓ ↓H␈↓␈↓α(PLUS (PLUS 2 3) 4)␈↓␈α>or␈α>␈↓α(PLUS 2 (PLUS 3 4))␈↓;␈α>or␈α>represent␈α>2*3*4+5+6␈α>as
␈↓ ↓H␈↓␈↓α(PLUS (TIMES 2 3 4) 5 6)␈↓.

␈↓ ↓H␈↓Write a new version of ␈↓αvalue␈↓ which can evaluate such n-ary representations of + and *.



␈↓ ↓H␈↓␈↓ ∧⎇␈↓↓More on polynomial evaluation␈↓


␈↓ ↓H␈↓Though␈α
it␈αshould␈α
be␈α
clear␈αthat␈α
the␈α
current␈α␈↓αvalue␈↓␈α
function␈α
does␈αperform␈α
the␈αappropriate␈α
calculation,
␈↓ ↓H␈↓it␈α⊂should␈α⊂be␈α⊂equally␈α⊂clear␈α∂that␈α⊂the␈α⊂class␈α⊂of␈α⊂expressions␈α∂which␈α⊂␈↓αvalue␈↓␈α⊂handles␈α⊂is␈α⊂not␈α∂particularly
␈↓ ↓H␈↓powerful.  We might wish to evaluate requests like:

␈↓ ↓H␈↓␈↓	A␈↓␈↓ β("What is the value of ␈↓αx*y + 2*z␈↓ when ␈↓αx=4, y=2,␈↓ and ␈↓αz=1␈↓?"

␈↓ ↓H␈↓Now␈αthe␈αfunction␈α␈↓αinstantiate␈↓,␈αrequested␈αin␈αproblem␈α2␈αabove,␈αoffers␈αone␈αsolution:␈αmake␈αa␈αnew␈αcopy
␈↓ ↓H␈↓of␈α⊃the␈α⊂representation␈α⊃of␈α⊃␈↓αx*y + 2*z␈↓␈α⊂with␈α⊃the␈α⊂variables␈α⊃physically␈α⊃replaced␈α⊂by␈α⊃their␈α⊃values.␈α⊂This
␈↓ ↓H␈↓would␈α∂result␈α∞in␈α∂a␈α∞representation␈α∂of␈α∞␈↓α4*2 +2*1␈↓,␈α∂and␈α∞this␈α∂new␈α∞expression␈α∂is␈α∞suitable␈α∂fare␈α∂for␈α∞␈↓αvalue␈↓.
␈↓ ↓H␈↓Computationally,␈α∪this␈α∪is␈α∪a␈α∀terrible␈α∪solution.␈α∪ ␈↓αinstantiate␈↓␈α∪will␈α∀go␈α∪through␈α∪the␈α∪structure␈α∀of␈α∪the
␈↓ ↓H␈↓expression␈α∀looking␈α∪for␈α∀instances␈α∀of␈α∪variables,␈α∀and␈α∪when␈α∀located,␈α∀will␈α∪replace␈α∀them␈α∀with␈α∪the
␈↓ ↓H␈↓appropriate␈α∞values.␈α∞␈↓αvalue␈↓␈α∞then␈α∞goes␈α∞through␈α∞the␈α∞structure␈α∞of␈α∞the␈α∞resulting␈α∞expression␈α
performing
␈↓ ↓H␈↓the␈α∂evaluation.␈α∂Clearly␈α∂what␈α∂is␈α∂desireable␈α∂is␈α∂a␈α∂function␈α∂␈↓αvalue␈↓λ'␈↓␈α∂combining␈α∂the␈α∂two␈α⊂processes:␈α∂the
␈↓ ↓H␈↓basic␈αstructure␈αof␈α␈↓αvalue␈↓λ'␈↓␈αis␈α
that␈αof␈αmild-mannered␈α␈↓αvalue␈↓,␈αbut␈α
when␈αa␈αvariable,␈αsay␈α␈↓αx␈↓,␈α
is␈αrecognized
␈↓ ↓H␈↓inside␈α␈↓αvalue␈↓λ'␈↓␈αthen␈αwe␈αwould␈αhope␈αthat␈αit␈αlooks␈αat␈αa␈αtable␈αlike␈αthat␈αexpected␈αby␈α␈↓αinstantiate␈↓,␈αfinds␈α␈↓αx␈↓
␈↓ ↓H␈↓and returns the value associated with the entry for ␈↓αx␈↓.

␈↓ ↓H␈↓Let's␈αformalize␈αour␈αintuitions␈αabout␈α
␈↓αvalue␈↓λ'␈↓.␈αIt␈αwill␈αbe␈αa␈αfunction␈α
of␈αtwo␈αarguments.␈αThe␈αfirst␈αwill␈α
be
␈↓ ↓H␈↓a␈αrepresentation␈αof␈αa␈αpolynomial;␈αthe␈αsecond␈αwill␈αbe␈αa␈αrepresentation␈αof␈αthe␈αtable␈αof␈αvariables␈αand
␈↓ ↓H␈↓values.␈αYou␈αmay␈αhave␈αnoticed␈αthat␈αthe␈αoriginal␈αversion␈αof␈α␈↓αvalue␈↓␈αin␈αfact␈αhandles␈αexpressions␈αwhich
␈↓ ↓H␈↓are␈α⊃not␈α⊃actually␈α⊃constant␈α⊃polynomials;␈α⊃␈↓α(2 + 3)*4␈↓␈α⊃for␈α⊃example.␈α⊃Since␈α⊃we␈α⊃will␈α⊃wish␈α⊃to␈α⊃apply␈α⊂our
␈↓ ↓H␈↓evaluation␈αfunctions␈αto␈αmore␈αgeneral␈αclasses␈αof␈αexpressions␈αwe␈αwill␈αcontinue,␈αindeed␈αencourage,␈αthis
␈↓ ↓H␈↓deception.␈α
 Regardless␈αof␈α
the␈αclass␈α
of␈α
expressions␈αwe␈α
wish␈αto␈α
examine,␈α
it␈αis␈α
the␈αstructure␈α
of␈αthe␈α
table
␈↓ ↓H␈↓which␈α
should␈α
be␈α
the␈α
first␈αorder␈α
of␈α
business.␈α
 An␈α
appropriate␈α
table,␈α␈↓αtbl␈↓,␈α
will␈α
be␈α
a␈α
set␈α
of␈αordered␈α
pairs
␈↓ ↓H␈↓␈↓α<name␈↓βi␈↓α, val␈↓βi␈↓α>␈↓;␈αthus␈αfor␈αthe␈αabove␈αexample␈αthe␈αtable␈α␈↓α{<x, 4>, <y, 2>, <z, 1>}␈↓␈αwould␈αsuffice.␈α Following
␈↓ ↓H␈↓our␈α∞dictum␈α
of␈α∞abstraction␈α
and␈α∞representation-independent␈α
programming,␈α∞we␈α
will␈α∞not␈α∞worry␈α
about
␈↓ ↓H␈↓the␈α
representational␈α
problems␈α
of␈α
such␈α
tables.␈α
We␈α
will␈α
simply␈α
assume␈α
that␈α
"tables"␈α
are␈α
instances␈αof
␈↓ ↓H␈↓an␈αabstract␈αdata␈αstructure␈αcalled␈α␈↓<table>␈↓,␈αand␈αwe␈αwill␈αonly␈αconcern␈αourselves␈αfor␈αthe␈αmoment␈αwith
␈↓ ↓H␈↓the␈αkinds␈αof␈αoperations␈αwe␈αneed␈αto␈αperform.␈α
We␈αwill␈αneed␈αtwo␈αselector␈αfunctions:␈α␈↓αname␈↓,␈αto␈αselect␈α
the
␈↓ ↓H␈↓variable-component␈αof␈αa␈αtable␈αentry;␈αand␈α␈↓αval␈↓,␈αto␈αselect␈αthe␈αvalue-component.␈αA␈α
complete␈αdiscussion
␈↓ ↓H␈↓of␈αsuch␈αa␈αdata␈αstructure␈αwould␈αentail␈αdiscussion␈αof␈αconstructors␈αand␈αrecognizers,␈αand␈αperhaps␈αother
␈↓ ↓H␈↓functions, but for the current ␈↓αvalue␈↓λ'␈↓ these two functions will suffice.

␈↓ ↓H␈↓␈↓αvalue␈↓λ'␈↓␈αwill␈αneed␈αa␈αtable-function,␈α␈↓αlocate␈↓,␈αto␈αlocate␈αan␈αappropriate␈αvariable-value␈αentry.␈α␈↓αlocate␈↓␈αwill␈αbe
␈↓ ↓H␈↓␈↓↓74  Applications␈↓ 53.6␈↓


␈↓ ↓H␈↓a␈α∀binary␈α∃function,␈α∀taking␈α∀one␈α∃argument,␈α∀␈↓αx␈↓,␈α∃representing␈α∀a␈α∀variable;␈α∃and␈α∀one␈α∃argument,␈α∀␈↓αtbl␈↓,
␈↓ ↓H␈↓representing␈αa␈αtable.␈α␈↓αlocate␈↓␈αwill␈αmatch␈α␈↓αx␈↓␈αagainst␈αthe␈α␈↓αname␈↓-part␈αof␈αeach␈αelement␈αin␈α␈↓αtbl␈↓;␈αif␈αa␈αmatch␈αis
␈↓ ↓H␈↓found then the corresponding ␈↓αval␈↓-part is returned. If no match is found then ␈↓αlocate␈↓ is undefined.

␈↓ ↓H␈↓So␈α
far,␈αlittle␈α
structure␈α
has␈αbeen␈α
imposed␈α
on␈αelements␈α
of␈α
␈↓<table>␈↓;␈αtables␈α
are␈α
either␈αempty␈α
or␈αnot;␈α
but
␈↓ ↓H␈↓if␈α∂a␈α∞table␈α∂is␈α∂non-empty␈α∞then␈α∂each␈α∂element␈α∞is␈α∂a␈α∞pair␈α∂with␈α∂recognizable␈α∞components␈α∂of␈α∂name␈α∞and
␈↓ ↓H␈↓value.␈α∂ However,␈α⊂the␈α∂specification␈α∂of␈α⊂algorithms␈α∂to␈α∂examine␈α⊂elements␈α∂of␈α∂␈↓<table>␈↓␈α⊂imposes␈α∂more
␈↓ ↓H␈↓structure␈αon␈αour␈αtables.␈α
 If␈αwe␈αwere␈αdealing␈αwith␈α
functions␈αthen␈αa␈αside␈α
condition␈αto␈αthe␈αeffect␈αthat␈α
a
␈↓ ↓H␈↓table␈α
had␈αno␈α
pairs␈α
with␈αduplicate␈α
first␈α
elements␈αwould␈α
be␈α
sufficient.␈α We,␈α
however,␈α
are␈αdealing␈α
with
␈↓ ↓H␈↓algorithms␈α
and␈α
therefore␈α
must␈α
describe␈α
a␈α
method␈αfor␈α
locating␈α
elements,␈α
and␈α
soon␈α
must␈α
describe␈αa
␈↓ ↓H␈↓method for adding elements.

␈↓ ↓H␈↓Recursion␈αis␈αthe␈αonly␈αmethod␈αwe␈αhave␈α
for␈αspecifying␈α␈↓αlocate␈↓,␈αand␈αrecursion␈αoperates␈αby␈α
decomposing
␈↓ ↓H␈↓a␈αstructure.␈αSets␈αare␈αnotorious␈αfor␈αtheir␈αlack␈αof␈αstructure;␈αthere␈αis␈αno␈αorder␈αto␈αthe␈αelements␈αof␈αa␈αset.
␈↓ ↓H␈↓But␈αif␈αwe␈αare␈αto␈αwrite␈αa␈αLISP␈αalgorithm␈αfor␈α␈↓αlocate␈↓,␈αthat␈αalgorithm␈αwill␈αhave␈αto␈αbe␈αrecursive␈αon␈αthe
␈↓ ↓H␈↓"structure"␈α∂of␈α∞␈↓αtbl␈↓,␈α∂and␈α∂so␈α∞we␈α∂impose␈α∞an␈α∂ordering␈α∂on␈α∞the␈α∂elements␈α∞of␈α∂that␈α∂table.␈α∞That␈α∂is,␈α∂we␈α∞will
␈↓ ↓H␈↓represent tables as ␈↓↓sequences␈↓. We know how to represent sequences in LISP: we use lists.

␈↓ ↓H␈↓With this introduction, here's ␈↓αlocate␈↓␈↓π 42␈↓:

␈↓ ↓H␈↓αlocate[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[eq[name[first[tbl]];x] → val[first[tbl]];
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → locate[x;rest[tbl]] ]

␈↓ ↓H␈↓The␈αeffect␈αof␈α␈↓αlocate␈↓␈αis␈αto␈αfind␈αthe␈α␈↓↓first␈↓␈αelement␈αof␈α␈↓αtbl␈↓␈αwhich␈αhas␈αa␈α␈↓αname␈↓-component␈αwhich␈αmatches
␈↓ ↓H␈↓␈↓αx␈↓.␈α
 Having␈α
found␈α
that␈α
match,␈α
the␈α
corresponding␈α
␈↓αval␈↓-part␈α
is␈α
returned.␈α
If␈α
there␈α
were␈α
other␈α
matches
␈↓ ↓H␈↓further␈α⊃along␈α⊃in␈α⊃the␈α⊃sequence␈α⊃␈↓αlocate␈↓␈α∩would␈α⊃not␈α⊃see␈α⊃them.␈α⊃ Other␈α⊃representations␈α⊃of␈α∩tables␈α⊃are
␈↓ ↓H␈↓certainly possible. This representation will be useful in later applications.
␈↓ ↓H␈↓And here's the new more powerful ␈↓αvalue␈↓λ'␈↓:

␈↓ ↓H␈↓αvalue␈↓λ'␈↓α[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX isvar[x] → locate[x;tbl];
␈↓ ↓H␈↓α␈↓ αX issum[x] → +[value␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];value␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]];
␈↓ ↓H␈↓α␈↓ αX isprod[x] → *[value␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];value␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]];
␈↓ ↓H␈↓α␈↓ αX isexpt[x] → ↑[value␈↓λ'␈↓α[arg␈↓β1␈↓α[x];tbl];value␈↓λ'␈↓α[arg␈↓β2␈↓α[x];tbl]] ]


␈↓ ↓H␈↓Notice␈αthat␈α␈↓αtbl␈↓␈αis␈αcarried␈αthrough␈αas␈αan␈αexplicit␈αargument␈αto␈α␈↓αvalue␈↓λ'␈↓␈αeven␈αthough␈αit␈αis␈αonly␈αaccessed
␈↓ ↓H␈↓when␈αa␈αvariable␈αis␈αrecognized.␈α Notice␈αtoo␈αthat␈αmuch␈αof␈αthe␈αstructure␈αof␈α␈↓αvalue␈↓λ'␈↓␈αis␈αquite␈αrepetitious;
␈↓ ↓H␈↓the␈α∂lines␈α∂which␈α∞handle␈α∂sums,␈α∂products,␈α∞and␈α∂exponentiation␈α∂are␈α∞identical␈α∂except␈α∂for␈α∂the␈α∞function
␈↓ ↓H␈↓which␈α∂finally␈α∂gets␈α∂applied␈α∂to␈α∂the␈α∂evaluated␈α∂arguments.␈α∂ That␈α∂is,␈α∂the␈α∂basic␈α∂structure␈α∂of␈α∂␈↓αvalue␈↓λ'␈↓␈α∞is

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 42␈↓␈α⊗The␈α∃obvious␈α⊗interpretation␈α∃of␈α⊗␈↓αtbl␈↓␈α∃as␈α⊗a␈α∃function␈α⊗implies␈α∃that␈α⊗␈↓αlocate␈↓␈α⊗represents␈α∃function
␈↓ ↓H␈↓application; i.e., ␈↓αlocate[x;tbl] ␈↓is␈↓α tbl(x)␈↓.
␈↓ ↓H␈↓␈↓↓3.6␈↓ λ"Evaluation of polynomials     75␈↓


␈↓ ↓H␈↓potentially␈α
of␈α
broader␈α
application␈α
than␈α
just␈α
the␈α
simple␈α
class␈α
of␈α
polynomials.␈α
 In␈α
keeping␈α
with␈α
our
␈↓ ↓H␈↓search for generality, let's pursue ␈↓αvalue␈↓λ'␈↓ a little further.

␈↓ ↓H␈↓What ␈↓αvalue␈↓λ'␈↓ says is:

␈↓ ↓H␈↓␈↓↓1.␈↓ The value of a constant is that constant.

␈↓ ↓H␈↓␈↓↓2.␈↓ The value of a variable is the current value associated with it in the table.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α⊂The␈α⊂value␈α⊂of␈α⊂a␈α⊂function␈α⊂applied␈α⊂to␈α⊂arguments␈α⊂is␈α⊂the␈α⊂result␈α⊂of␈α⊂applying␈α⊂the␈α⊂function␈α⊂to␈α∂the
␈↓ ↓H␈↓evaluated␈αarguments.␈αIt␈αjust␈αturns␈αout␈αthat␈αthe␈αonly␈αfunctions␈α␈↓αvalue␈↓λ'␈↓␈αknows␈αabout␈αare␈αbinary␈αsums,
␈↓ ↓H␈↓products, and exponentiation.

␈↓ ↓H␈↓Let's clean up ␈↓αvalue␈↓λ'␈↓ a bit.

␈↓ ↓H␈↓αvalue␈↓λ'␈↓α[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX isvar[x] → locate[x;tbl];
␈↓ ↓H␈↓α␈↓ αX isfun_args[x] → apply[fun[x];eval_args[args[x];tbl]] ]

␈↓ ↓H␈↓The␈α
changes␈α
are␈α∞in␈α
the␈α
last␈α∞branch␈α
of␈α
the␈α∞conditional.␈α
We␈α
have␈α∞a␈α
new␈α
recognizer,␈α∞␈↓αisfun_args␈↓␈α
to
␈↓ ↓H␈↓recognize␈α
function␈α
application.␈α
We␈α
have␈α
two␈α
new␈α
selector␈α
functions;␈α
␈↓αfun␈↓␈α
selects␈α
the␈α
representation␈α
of
␈↓ ↓H␈↓the␈αfunction␈α-- sum,␈αproduct,␈αor␈αpower␈αin␈αthe␈αsimple␈αcase;␈α␈↓αargs␈↓␈αselects␈αthe␈αarguments␈αor␈αparameters
␈↓ ↓H␈↓to␈α⊂the␈α⊃function␈α⊂-- in␈α⊃this␈α⊂case␈α⊃all␈α⊂functions␈α⊂are␈α⊃binary.␈α⊂We␈α⊃have␈α⊂two␈α⊃new␈α⊂functions␈α⊃to␈α⊂define:
␈↓ ↓H␈↓␈↓αeval_args␈↓,␈α∂which␈α∂is␈α∂supposed␈α∂to␈α∂evaluate␈α∂the␈α∂arguments␈α∂using␈α∂␈↓αtbl␈↓␈α∂to␈α∂find␈α∂values␈α∂for␈α∂any␈α∂of␈α∞the
␈↓ ↓H␈↓variables; and ␈↓αapply␈↓, which is used to perform the desired operation on the evaluated arguments.

␈↓ ↓H␈↓Again␈α∂we␈α∂are␈α∂trying␈α∂to␈α∂remain␈α⊂as␈α∂representation-free␈α∂as␈α∂possible:␈α∂thus␈α∂the␈α∂generalization␈α⊂of␈α∂the
␈↓ ↓H␈↓algorithm␈α␈↓αvalue␈↓λ'␈↓,␈αand␈αthus␈αthe␈αcare␈αin␈αpicking␈αrepresentations␈αfor␈αthe␈αdata␈αstructures.␈α We␈αneed␈αto
␈↓ ↓H␈↓make␈α
another␈αdata␈α
structure␈αdecision␈α
now;␈α
when␈αwriting␈α
the␈αfunction␈α
␈↓αeval_args␈↓,␈α
we␈αwill␈α
be␈αgiving␈α
a
␈↓ ↓H␈↓recursive␈αalgorithm.␈α This␈αalgorithm␈αwill␈αbe␈αrecursive␈αon␈αthe␈αstructure␈αof␈αthe␈αfirst␈αargument,␈α
which
␈↓ ↓H␈↓is␈α
a␈α
representation␈α
of␈αthe␈α
arguments␈α
to␈α
the␈αfunction.␈α
In␈α
contrast␈α
to␈αour␈α
position␈α
when␈α
writing␈αthe
␈↓ ↓H␈↓function␈α
␈↓αlocate␈↓,␈αthere␈α
␈↓↓is␈↓␈αa␈α
natural␈αstructure␈α
on␈αthe␈α
arguments␈αto␈α
a␈αfunction:␈α
they␈αform␈α
a␈αsequence.
␈↓ ↓H␈↓That␈α⊂is␈α∂␈↓αf[1;2;3]␈↓␈α⊂is␈α⊂typically␈α∂not␈α⊂the␈α⊂same␈α∂as␈α⊂␈↓αf[3;2;1]␈↓␈α⊂or␈α∂␈↓αf␈↓␈α⊂applied␈α⊂to␈α∂any␈α⊂other␈α⊂permutation␈α∂of
␈↓ ↓H␈↓{1, 2, 3}.␈α⊃Thus␈α∩writing␈α⊃␈↓αeval_args␈↓␈α⊃as␈α∩a␈α⊃function,␈α∩recursive␈α⊃on␈α⊃the␈α∩sequence-structure␈α⊃of␈α∩its␈α⊃first
␈↓ ↓H␈↓argument, is quite expected. Here is ␈↓αeval_args␈↓:

␈↓ ↓H␈↓αeval_args[args;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[null[args] → ();
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → concat[value␈↓λ'␈↓α[first[args];tbl]; eval_args[rest[args];tbl]] ]

␈↓ ↓H␈↓Notice␈αthat␈αwe␈αhave␈αwritten␈α␈↓αeval_args␈↓␈αwithout␈αany␈αbias␈αtoward␈αbinary␈αfunctions;␈αit␈αwill␈αevaluate␈αa
␈↓ ↓H␈↓sequence of arbitrary length, returning a sequence of the evaluated arguments.
␈↓ ↓H␈↓␈↓↓76  Applications␈↓ 53.6␈↓



␈↓ ↓H␈↓There␈αshould␈αbe␈αno␈αreal␈α
surprises␈αin␈α␈↓αapply␈↓;␈αit␈αgets␈αthe␈α
representation␈αof␈αthe␈αfunction␈αname␈αand␈α
the
␈↓ ↓H␈↓sequence of evaluated arguments and does its job:

␈↓ ↓H␈↓αapply[fn; evargs] <=
␈↓ ↓H␈↓α␈↓ αX[issum[fn] → +[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓ ↓H␈↓α␈↓ αX isprod[fn] → *[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓ ↓H␈↓α␈↓ αX isexpt[fn] → ↑[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]] ]

␈↓ ↓H␈↓Notice␈αthat␈αif␈αwe␈αshould␈αdesire␈αto␈αrecognize␈αmore␈αfunctions␈αthen␈αwe␈αneed␈αonly␈αmodify␈α␈↓αapply␈↓.␈αThat
␈↓ ↓H␈↓would␈α
be␈α
a␈α∞short-term␈α
solution,␈α
but␈α∞if␈α
we␈α
wished␈α∞to␈α
do␈α
reasonable␈α∞computations␈α
we␈α
would␈α∞like␈α
a
␈↓ ↓H␈↓more␈α
general␈α
function-definition␈α
facility.␈α
Such␈α
a␈α
feature␈α
would␈α
allow␈α
new␈α
functions␈α
to␈α∞be␈α
defined
␈↓ ↓H␈↓during␈α
a␈α
computation;␈α
then␈αwhen␈α
an␈α
application␈α
of␈αthat␈α
function␈α
were␈α
needed,␈α
the␈α␈↓αvalue␈↓-function
␈↓ ↓H␈↓would␈α∩find␈α∪that␈α∩definition␈α∪and␈α∩apply␈α∪␈↓↓it␈↓␈α∩in␈α∪a␈α∩manner␈α∪analogous␈α∩to␈α∪the␈α∩way␈α∪the␈α∩pre-defined
␈↓ ↓H␈↓functions␈αare␈α
applied.␈αHow␈αfar␈α
away␈αare␈αwe␈α
from␈αthis␈αmore␈α
desirable␈αsuper-␈↓αvalue␈↓?␈α Well␈α
␈↓αvalue␈↓λ'␈↓␈αis
␈↓ ↓H␈↓already␈α
well-endowed␈α
with␈αa␈α
mechanism␈α
for␈αlocating␈α
values;␈α
perhaps␈αwe␈α
can␈α
exploit␈αthis␈α
judiciously
␈↓ ↓H␈↓placed␈α∞code.␈α
 In␈α∞what␈α
context␈α∞would␈α∞we␈α
be␈α∞interested␈α
in␈α∞locating␈α
function␈α∞definitions?␈α∞ Here's␈α
an
␈↓ ↓H␈↓example:

␈↓ ↓H␈↓␈↓	B␈↓␈↓ β("What is the value of ␈↓αf[4;2;1]␈↓ when ␈↓αf[x;y;z] <= x*y + 2*z␈↓?"

␈↓ ↓H␈↓Notice␈α
that␈α
if␈α
we␈α
have␈α
a␈α
means␈α
of␈α
recovering␈α
the␈α
definition␈α
of␈α
␈↓αf␈↓,␈α
then␈α
we␈α
can␈α
reduce␈α
the␈αproblem␈α
to
␈↓ ↓H␈↓␈↓	A␈↓␈α∂of␈α∂page 73.␈α∞ We␈α∂will␈α∂utilize␈α∞the␈α∂table-mechanism,␈α∂and␈α∞therefore␈α∂will␈α∂use␈α∞␈↓αlocate␈↓␈α∂to␈α∂retrieve␈α∞the
␈↓ ↓H␈↓definition␈α∞of␈α
the␈α∞function␈α
␈↓αf␈↓.␈α∞ In␈α∞our␈α
prior␈α∞applications␈α
of␈α∞␈↓αlocate␈↓␈α∞we␈α
would␈α∞find␈α
a␈α∞constant␈α∞as␈α
the
␈↓ ↓H␈↓associated␈α
value.␈α
Now,␈α∞given␈α
the␈α
name␈α∞␈↓αf␈↓,␈α
we␈α
would␈α∞expect␈α
to␈α
find␈α∞the␈α
definition␈α
of␈α∞the␈α
function.
␈↓ ↓H␈↓The␈αquestion␈αthen,␈αis␈αhow␈αdo␈αwe␈αrepresent␈αthe␈αdefinition␈αof␈α␈↓αf␈↓?␈α Certainly␈αthe␈αbody␈αof␈αthe␈αfunction,
␈↓ ↓H␈↓␈↓αx*y + 2*z␈↓,␈α
is␈αone␈α
of␈α
the␈αnecessary␈α
ingredients,␈αbut␈α
is␈α
that␈αall?␈α
Given␈α
the␈αexpression␈α
␈↓αx*y + 2*z␈↓␈αcan␈α
we
␈↓ ↓H␈↓successfully␈α∂compute␈α∂␈↓αf[4;2;1]␈↓?␈α∂ Not␈α∞yet;␈α∂we␈α∂need␈α∂to␈α∞know␈α∂the␈α∂correspondence␈α∂between␈α∂the␈α∞values
␈↓ ↓H␈↓␈↓α1, 2, 4␈↓␈αand␈αthe␈αvariables,␈α␈↓αx,␈αy,␈αz␈↓.␈αThat␈αinformation␈αis␈αpresent␈αin␈αour␈αnotation␈α␈↓αf[x;y;z] <= ...␈↓,␈αand␈αis␈αa
␈↓ ↓H␈↓crucial␈αpart␈αof␈αthe␈αdefinition␈αof␈α␈↓αf␈↓.␈α That␈αis,␈αthe␈α␈↓↓order␈↓␈αof␈αthe␈αvariables␈αappearing␈αafter␈αthe␈αfunction
␈↓ ↓H␈↓name is an integral part of the definition: ␈↓αf[y;z;x] <= x*y +2*z␈↓ defines a different function.

␈↓ ↓H␈↓Since␈α⊂we␈α⊂are␈α⊂now␈α⊂talking␈α∂about␈α⊂␈↓↓representations␈↓␈α⊂of␈α⊂functions,␈α⊂we␈α∂are␈α⊂getting␈α⊂into␈α⊂the␈α⊂realm␈α∂of
␈↓ ↓H␈↓abstract␈α
data␈α
structures.␈α
We␈αhave␈α
a␈α
reasonable␈α
understanding␈αnow␈α
of␈α
the␈α
essential␈α
components␈αof
␈↓ ↓H␈↓such a representation.  For our purposes, a function has three parts:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓ A name; ␈↓αf␈↓ in the current example.

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓ A variable list; ␈↓α[x;y;z]␈↓ here.

␈↓ ↓H␈↓␈↓ αh␈↓↓3.␈↓ A body; ␈↓αx*y + 2*z␈↓ in the example.

␈↓ ↓H␈↓We␈α
do␈α
not␈α
need␈α
a␈α
complete␈α
study␈α
of␈α
representations␈α
of␈α
functions.␈α
For␈α
our␈α
purposes␈α
we␈α
can␈α
assume␈α
a
␈↓ ↓H␈↓representation␈α⊃exists,␈α⊃and␈α⊂that␈α⊃we␈α⊃are␈α⊂supplied␈α⊃with␈α⊃three␈α⊂selectors␈α⊃to␈α⊃retrieve␈α⊃the␈α⊂components
␈↓ ↓H␈↓mentioned above.
␈↓ ↓H␈↓␈↓↓3.6␈↓ λ"Evaluation of polynomials     77␈↓


␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓␈α∂␈↓αname␈↓␈α∂selects␈α∂the␈α∞name␈α∂component␈α∂from␈α∂the␈α∞representation.␈α∂We␈α∂have␈α∂actually␈α∞seen
␈↓ ↓H␈↓␈↓ αh␈↓αname␈↓ before in the definition ␈↓αlocate␈↓ on page 74.

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓␈α∞␈↓αvarlist␈↓␈α∞selects␈α∞the␈α∞list␈α∞of␈α∞variables␈α∞from␈α∞the␈α∞representation.␈α∞ We␈α∞have␈α∞already␈α∞seen
␈↓ ↓H␈↓␈↓ αhthat␈αthe␈α
natural␈αway␈α
to␈αthink␈αabout␈α
this␈αcomponent␈α
is␈αas␈αa␈α
sequence.␈αThus␈α
the␈αname:
␈↓ ↓H␈↓␈↓ αh␈↓αvarlist␈↓.

␈↓ ↓H␈↓␈↓ αh␈↓↓3.␈↓ ␈↓αbody␈↓ selects the expression which is the content of the definition.

␈↓ ↓H␈↓Given␈α⊃a␈α⊂function␈α⊃represented␈α⊂in␈α⊃the␈α⊂table␈α⊃according␈α⊂to␈α⊃these␈α⊂conventions,␈α⊃how␈α⊂do␈α⊃we␈α⊃use␈α⊂the
␈↓ ↓H␈↓information␈α↔to␈α↔effect␈α↔the␈α↔evaluation␈α↔of␈α⊗something␈α↔like␈α↔␈↓αf[4;2;1]␈↓?␈α↔ First␈α↔␈↓αvalue␈↓λ'␈↓␈α↔will␈α↔see␈α⊗the
␈↓ ↓H␈↓representation␈α∞of␈α
␈↓αf[4;2;1]␈↓;␈α∞it␈α
should␈α∞recognize␈α
an␈α∞instance␈α
of␈α∞function-application␈α
at␈α∞the␈α
following
␈↓ ↓H␈↓line of ␈↓αvalue␈↓λ'␈↓:
␈↓ ↓H␈↓α␈↓ ∧αisfun_args[x] → apply[fun[x];eval_args[args[x];tbl]]


␈↓ ↓H␈↓This should cause an evaluation of the arguments and then pass on the hard work to ␈↓αapply␈↓.

␈↓ ↓H␈↓Clever␈α
␈↓αapply␈↓␈αshould␈α
soon␈αrealize␈α
that␈α␈↓αf␈↓␈α
is␈αnot␈α
the␈α
name␈αof␈α
a␈αknown␈α
function.␈αIt␈α
should␈αthen␈α
extract
␈↓ ↓H␈↓the␈αdefinition␈αof␈α␈↓αf␈↓␈αfrom␈αthe␈αtable;␈αassociate␈αthe␈αevaluated␈αarguments␈α(␈↓α4, 2, 1␈↓)␈αwith␈αthe␈α
variables␈αof
␈↓ ↓H␈↓the␈αvariable␈αlist␈α(␈↓αx, y, z␈↓),␈α
making␈αa␈αnew␈αtable␈α
with␈αname-value␈αpairs␈α(␈↓α<x, 4>, <y, 2>, <z, 1>␈↓).␈α Now␈α
we
␈↓ ↓H␈↓are␈α∃back␈α∃to␈α∀the␈α∃setting␈α∃of␈α∀problem␈α∃␈↓	A␈↓␈α∃of␈α∀page 73.␈α∃ We␈α∃should␈α∀ask␈α∃␈↓αvalue␈↓λ'␈↓␈α∃to␈α∃evaluate␈α∀the
␈↓ ↓H␈↓␈↓αbody␈↓-component␈α∂of␈α∂the␈α⊂function␈α∂using␈α∂the␈α⊂new␈α∂␈↓αtbl␈↓.␈α∂ This␈α∂works␈α⊂fine␈α∂for␈α∂␈↓αx␈↓,␈α⊂␈↓αy␈↓,␈α∂and␈α∂␈↓αz␈↓;␈α⊂within␈α∂the
␈↓ ↓H␈↓evaluation␈αof␈αthe␈αbody␈αof␈α␈↓αf␈↓␈αwe␈αwill␈αfind␈αthe␈αright␈αbindings␈αfor␈αthese␈αvariables.␈αBut␈αwe␈αmight␈αalso
␈↓ ↓H␈↓need␈α∂some␈α∂information␈α∂from␈α∂the␈α∂original␈α∂␈↓αtbl␈↓.␈α∂The␈α∂evalaution␈α∂of␈α∂the␈α∂body␈α∂of␈α∂␈↓αf␈↓␈α∂might␈α∂entail␈α∞the
␈↓ ↓H␈↓application of some function definition present in ␈↓αtbl␈↓. For example, the representation of
␈↓ ↓H␈↓␈↓ βx"what is ␈↓αg[2]␈↓ where ␈↓αg[x] <= x+s[x];␈↓ and ␈↓αs[x] <= x*x␈↓?"

␈↓ ↓H␈↓Within␈α
the␈α
body␈α
of␈α␈↓αg␈↓␈α
we␈α
need␈α
the␈α
definition␈αof␈α
␈↓αs␈↓.␈α
Therefore,␈α
instead␈αof␈α
building␈α
a␈α
new␈α
table␈αwe
␈↓ ↓H␈↓will␈αadd␈α
the␈αnew␈α
bindings␈αto␈αthe␈α
front␈αof␈α
the␈αold␈α
table.␈αSince␈α␈↓αlocate␈↓␈α
begins␈αits␈α
search␈αfrom␈αthe␈α
front
␈↓ ↓H␈↓of␈αthe␈αtable␈αwe␈αwill␈αbe␈αassured␈αof␈αfinding␈αthe␈αnew␈αbindings;␈αsince␈αthe␈αold␈αtable␈αis␈αstill␈αaccessible␈α
we
␈↓ ↓H␈↓are assured of finding any necessary previous bindings.

␈↓ ↓H␈↓Now␈α
we␈αshould␈α
be␈α
able␈αto␈α
go␈α
off␈αand␈α
create␈αa␈α
new␈α
␈↓αvalue␈↓λ''␈↓.␈α Looking␈α
at␈α
the␈αfiner␈α
detail␈α
of␈α␈↓αvalue␈↓λ'␈↓
␈↓ ↓H␈↓and␈α
␈↓αapply␈↓,␈α
we␈α
can␈αsee␈α
a␈α
few␈α
other␈αmodifications␈α
need␈α
to␈α
be␈α
made.␈α ␈↓αapply␈↓λ'␈↓␈α
will␈α
to␈α
␈↓αlocate␈↓␈αthe␈α
function
␈↓ ↓H␈↓definition␈αand␈αthus␈α
␈↓αtbl␈↓␈αshould␈αbe␈α
included␈αas␈αa␈αthird␈α
argument␈αto␈α␈↓αapply␈↓λ'␈↓.␈α
That␈αis,␈αinside␈α␈↓αapply␈↓λ'␈↓␈α
we
␈↓ ↓H␈↓will have:

␈↓ ↓H␈↓␈↓ ∧B␈↓αisfun[fn] → apply␈↓λ'␈↓α[locate[fn;tbl];eargs;tbl]␈↓;

␈↓ ↓H␈↓After␈α∞␈↓αlocate␈↓␈α
has␈α∞done␈α
its␈α∞work,␈α
this␈α∞line␈α
(above)␈α∞will␈α
invoke␈α∞␈↓αapply␈↓λ'␈↓␈α
with␈α∞a␈α
function␈α∞definition␈α
as
␈↓ ↓H␈↓first argument. We'd better prepare ␈↓αapply␈↓λ'␈↓ for such an eventuality with the following addition:

␈↓ ↓H␈↓α␈↓ βfisdef[fn] → value␈↓λ''␈↓α[body[fn];newtbl[varlist[fn];eargs;tbl]];

␈↓ ↓H␈↓What does this incredible line say? It says
␈↓ ↓H␈↓␈↓↓78  Applications␈↓ 53.6␈↓


␈↓ ↓H␈↓␈↓ α_"Evaluate␈α∞the␈α
body␈α∞of␈α∞the␈α
function␈α∞using␈α
a␈α∞new␈α∞table␈α
manufactured␈α∞from␈α
the␈α∞old␈α∞table␈α
by
␈↓ ↓H␈↓␈↓ α_adding the pairings of the elements of the variable list with the evaluated arguments."

␈↓ ↓H␈↓It␈α∂also␈α∂says␈α∂we'd␈α∂better␈α∂write␈α∂␈↓αnewtbl␈↓.␈α∂This␈α∂function␈α∂will␈α∂be␈α∂making␈α∂a␈α∂new␈α∂table␈α∂by␈α⊂adding␈α∂new
␈↓ ↓H␈↓name-value␈α∩pairs␈α∩to␈α⊃an␈α∩existing␈α∩table.␈α∩ So␈α⊃we'd␈α∩better␈α∩name␈α∩a␈α⊃constructor␈α∩to␈α∩generate␈α∩a␈α⊃new
␈↓ ↓H␈↓name-value pair:

␈↓ ↓H␈↓␈↓αmkent␈↓␈αis␈αthe␈αconstructor␈αto␈αmake␈αnew␈αentries.␈αIt␈αwill␈αtake␈αtwo␈αarguments:␈αthe␈αfirst␈αwill␈αbe␈αthe␈αname,
␈↓ ↓H␈↓␈↓ α_the second will be the value.

␈↓ ↓H␈↓Since␈αwe␈α
have␈αassumed␈α
that␈αthe␈α
structure␈αof␈α
tables,␈αvariable-lists,␈α
and␈αcalling␈α
sequences␈αto␈α
functions
␈↓ ↓H␈↓are ␈↓↓all␈↓ sequences, we will write ␈↓αnewtbl␈↓ assuming this representation.

␈↓ ↓H␈↓αnewtbl[vars;vals;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[null[vars] → tbl;
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → concat[mkent[first[vars];first[vals]];newtbl[rest[vars];rest[vals];tbl]] ]

␈↓ ↓H␈↓And finally here's the new ␈↓αvalue␈↓λ''␈↓α-apply␈↓λ'␈↓ pair:

␈↓ ↓H␈↓αvalue␈↓λ''␈↓α[x;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[isconstant[x] → x;
␈↓ ↓H␈↓α␈↓ αX isvar[x] → locate[x;tbl];
␈↓ ↓H␈↓α␈↓ αX isfun_args[x] → apply␈↓λ'␈↓α[fun[x];eval_args[args[x];tbl];tbl] ]

␈↓ ↓H␈↓αapply␈↓λ'␈↓α[fn;evargs;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[issum[fn] → +[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓ ↓H␈↓α␈↓ αX isprod[fn] → *[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓ ↓H␈↓α␈↓ αX isexpt[fn] → ↑[arg␈↓β1␈↓α[evargs];arg␈↓β2␈↓α[evargs]];
␈↓ ↓H␈↓α␈↓ αX isfun[fn] → apply␈↓λ'␈↓α[locate[fn;tbl];evargs;tbl];
␈↓ ↓H␈↓α␈↓ αX isdef[fn] → value␈↓λ''␈↓α[body[fn];newtbl[varlist[fn];evargs;tbl]] ]

␈↓ ↓H␈↓αeval_args[args;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[null[args] → ()
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → concat[value␈↓λ'␈↓α[first[args];tbl]; eval_args[rest[args];tbl]] ]

␈↓ ↓H␈↓Let's␈α
go␈α
through␈α
a␈α
complete␈α∞massaging␈α
of␈α
␈↓	B␈↓␈α
of␈α
page 76.␈α
 As␈α∞before␈α
we␈α
will␈α
use␈α
␈↓
R␈↓␈α
as␈α∞a␈α
mapping
␈↓ ↓H␈↓from expressions to representations.  Thus we want to pursue:

␈↓ ↓H␈↓␈↓ βf␈↓αvalue␈↓λ''␈↓α[␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α{ <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓α]␈↓. 

␈↓ ↓H␈↓Before␈αwe␈αbegin,␈αlet␈αus␈αdenote␈αthe␈αinitial␈αsymbol␈αtable,␈α␈↓
R␈↓∞(␈↓α{ <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓␈αas␈α␈↓αinit␈↓.␈αThis
␈↓ ↓H␈↓will␈αsimplify␈αmany␈αof␈αthe␈αequations.␈α Notice␈αthat␈αour␈αrepresentation␈αof␈α␈↓αf␈↓␈αin␈α␈↓αinit␈↓␈αhas␈α
associated␈αthe
␈↓ ↓H␈↓variable␈α∞list␈α∂␈↓α[x;y;z]␈↓␈α∞with␈α∂the␈α∞body␈α∂of␈α∞the␈α∞function.␈α∂Thus␈α∞␈↓αlocate␈↓,␈α∂operating␈α∞on␈α∂this␈α∞table␈α∂with␈α∞the
␈↓ ↓H␈↓name ␈↓αf␈↓, will return a representation of ␈↓α[[x;y;z] x*y + 2*z]␈↓.

␈↓ ↓H␈↓␈↓αisfun_args␈↓ should be satisfied and thus the computation should reduce to:
␈↓ ↓H␈↓␈↓↓3.6␈↓ λ"Evaluation of polynomials     79␈↓


␈↓ ↓H␈↓␈↓ β¬␈↓αapply␈↓λ'␈↓α[fun[ ␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α ];eval_args[args[ ␈↓
R␈↓∞(␈↓α f[4;2;1] ␈↓∞)␈↓α ]; init ]␈↓ or: 

␈↓ ↓H␈↓␈↓ βp␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ;eval_args[ ␈↓
R␈↓∞(␈↓α [4;2;1] ␈↓∞)␈↓α; init]; init ] 

␈↓ ↓H␈↓αeval_args␈↓ will build a sequence of the evaluated arguments: ␈↓α(4, 2, 1)␈↓, resulting in:

␈↓ ↓H␈↓␈↓ ∧}␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ;(4, 2, 1) ; init ] 

␈↓ ↓H␈↓αapply␈↓λ'␈↓ should decide that ␈↓αf␈↓ satisfies ␈↓αisfun␈↓ giving:

␈↓ ↓H␈↓␈↓ ∧_␈↓αapply␈↓λ'␈↓α[ value␈↓λ''␈↓α[ ␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α ; init ]; (4, 2, 1) ; init ]␈↓ 

␈↓ ↓H␈↓The␈α
computation␈α
of:␈α
value␈↓λ''␈↓[␈α
␈↓
R␈↓∞(␈↓α␈α
f␈α
␈↓∞)␈↓α␈α
;␈α
init␈α]␈↓␈α
is␈α
interesting:␈α
␈↓αvalue␈↓␈α
should␈α
believe␈α
that␈α
␈↓αf␈↓␈α
is␈αa␈α
function
␈↓ ↓H␈↓name and therefore ␈↓αlocate␈↓ will be called and retrieve the definition.

␈↓ ↓H␈↓␈↓ β¬␈↓αapply␈↓λ'␈↓α[ ␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α ; (4, 2, 1) ; init ]␈↓ should be the result. 

␈↓ ↓H␈↓This␈αfirst␈αargument␈αshould␈αnot␈αmake␈α␈↓αapply␈↓λ'␈↓␈αunhappy.␈αIt␈αshould␈αrealize␈αthat␈α␈↓αisdef␈↓␈αis␈αsatisfied,␈αand
␈↓ ↓H␈↓thus:

␈↓ ↓H␈↓␈↓ ↓[␈↓αvalue␈↓λ''␈↓α[body[ ␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α ];newtbl[varlist[ ␈↓
R␈↓∞(␈↓α [[x;y;z] x*y + 2*z] ␈↓∞)␈↓α ];(4,2,1);init]]␈↓ or: 

␈↓ ↓H␈↓␈↓ β>␈↓αvalue␈↓λ''␈↓α[ ␈↓
R␈↓∞(␈↓α [x*y + 2*z] ␈↓∞)␈↓α ;newtbl[ ␈↓
R␈↓∞(␈↓α [x;y;z] ␈↓∞)␈↓α ;(4,2,1);init]]␈↓ 

␈↓ ↓H␈↓after␈α⊗␈↓αbody␈↓␈α⊗and␈α↔␈↓αvarlist␈↓␈α⊗are␈α⊗finished.␈α↔ But␈α⊗␈↓
R␈↓∞(␈↓α [x;y;z] ␈↓∞)␈↓␈α⊗is␈α↔␈↓α(␈↓
R␈↓∞(␈↓α x ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α y ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α z ␈↓∞)␈↓α)␈↓,␈α⊗and
␈↓ ↓H␈↓therefore the computation of ␈↓αnewtbl␈↓ will build a new table with entries for ␈↓αx, y, ␈↓ and ␈↓αz␈↓ on the front:

␈↓ ↓H␈↓␈↓ β|␈↓
R␈↓∞(␈↓α{ <x, 4>, <y, 2>, <z, 1>, <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓. 

␈↓ ↓H␈↓Thus we call ␈↓αvalue␈↓λ''␈↓ with:

␈↓ ↓H␈↓␈↓ α>␈↓αvalue␈↓λ''␈↓α[ ␈↓
R␈↓∞(␈↓α [x*y + 2*z] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α{ <x, 4>, <y, 2>, <z, 1>, <f, [[x;y;z] x*y + 2*z]> }␈↓∞)␈↓α ]␈↓. 

␈↓ ↓H␈↓Now we're back at problem ␈↓	A␈↓ of page 73.



␈↓ ↓H␈↓␈↓ ¬Q␈↓↓Time to take stock␈↓


␈↓ ↓H␈↓We␈αhave␈αwritten␈αa␈αreasonably␈αsophisticated␈αalgorithm␈αhere.␈αIt␈αcovers␈αevaluation␈αof␈αa␈αlarge␈αclass␈αof
␈↓ ↓H␈↓arithmetic functions. We should examine the results quite carefully.

␈↓ ↓H␈↓First␈α⊂notice␈α⊂that␈α∂we␈α⊂have␈α⊂written␈α⊂the␈α∂algorithm␈α⊂with␈α⊂almost␈α∂no␈α⊂concern␈α⊂for␈α⊂representation.␈α∂We
␈↓ ↓H␈↓␈↓↓assume␈↓␈α
that␈α
representations␈α
are␈α
available␈α
for␈αsuch␈α
varied␈α
things␈α
as␈α
arithmetic␈α
expressions,␈αtables,
␈↓ ↓H␈↓␈↓↓80  Applications␈↓ 53.6␈↓


␈↓ ↓H␈↓calls␈αon␈αfunctions,␈α
and␈αeven␈αfunction␈αdefinitions.␈α
Very␈αseldom␈αdid␈α
we␈αcommit␈αourselves␈αto␈α
anything
␈↓ ↓H␈↓close␈α∞to␈α∞a␈α∞concrete␈α∞representation,␈α∞and␈α∞only␈α∞then␈α∞with␈α∞great␈α∞reluctance.␈α∞It␈α∞was␈α∞with␈α∞some␈α∞sadness
␈↓ ↓H␈↓that␈αwe␈αimposed␈αa␈αsequencing␈αon␈αelements␈αof␈αtables.␈α Variable␈αlists␈αand␈αcalling␈αsequences␈αwere␈αnot
␈↓ ↓H␈↓as␈αtraumatic;␈αwe␈αclaimed␈αtheir␈αnatural␈αstructure␈αwas␈αa␈αsequence.␈αAs␈αalways,␈αif␈αwe␈αwish␈αto␈αrun␈αthese
␈↓ ↓H␈↓programs␈α
on␈α
a␈α
machine␈α
we␈α
must␈α
supply␈α
some␈α
representations,␈α
but␈α
even␈α
then␈α
the␈α
representations␈α
will
␈↓ ↓H␈↓only interface with our algorithms at the constructors, selectors and recognizers.

␈↓ ↓H␈↓We␈αhave␈α
made␈αsome␈αmore␈α
serious␈αrepresentational␈αdecisions␈α
in␈αthe␈αstructure␈α
of␈αthe␈α␈↓↓algorithm␈↓.␈α
 We
␈↓ ↓H␈↓have␈α
encoded␈α
a␈α
version␈α
of␈α
the␈α
␈↓	CBV␈↓-scheme␈α∞of␈α
page 19.␈α
We␈α
have␈α
seen␈α
what␈α
kinds␈α∞of␈α
difficulties
␈↓ ↓H␈↓␈↓↓that␈α␈↓␈αcan␈αget␈αus␈αinto.␈αWe␈αwill␈αspend␈αa␈αlarge␈αamount␈αof␈αtime␈αin␈αChapter 4␈αdiscussing␈αthe␈αproblems
␈↓ ↓H␈↓of evaluation␈↓π 43␈↓.

␈↓ ↓H␈↓Finally,␈αour␈αdecisions␈αon␈αthe␈αdata␈αstructures␈αand␈αthe␈αalgorithms␈αwere␈αnot␈αmade␈αindependently.␈αFor
␈↓ ↓H␈↓example,␈αthere␈αis␈αstrong␈αinteraction␈αbetween␈αour␈αrepresentation␈αof␈αtables␈αand␈αthe␈αalgorithms,␈α␈↓αlocate␈↓
␈↓ ↓H␈↓and␈α
␈↓αnewtbl␈↓␈α
which␈αmanipulate␈α
them.␈α
We␈α
should␈αask␈α
how␈α
much␈α
of␈αthis␈α
interaction␈α
is␈α
inherent␈αand
␈↓ ↓H␈↓how␈α∞much␈α∞is␈α∞gratuitous.␈α∞For␈α∞example␈α∞we␈α∞have␈α∞remarked␈α∞that␈α∞our␈α∞representation␈α∞can␈α∂have␈α∞pairs
␈↓ ↓H␈↓with␈αduplicate␈αfirst␈αelements.␈αIt␈αis␈αthe␈αresponsibility␈αof␈α␈↓αlocate␈↓␈αto␈αsee␈αthat␈αwe␈αfind␈αthe␈αexpected␈αpair.
␈↓ ↓H␈↓If␈αwe␈αwrote␈α␈↓αlocate␈↓␈αto␈αsearch␈αfrom␈αright␈αto␈αleft,␈αwe␈αcould␈αget␈αthe␈αwrong␈αpair.␈αWe␈α␈↓↓could␈↓␈αwrite␈α␈↓αnewtbl␈↓
␈↓ ↓H␈↓to be more selective; it could manufacture a table without such duplications:

␈↓ ↓H␈↓αnewtbl[vars;vals;tbl] <=
␈↓ ↓H␈↓α␈↓ αX[null[tbl] →␈↓ βh[null[vars] → ();
␈↓ ↓H␈↓α␈↓ αX␈↓ βh ␈↓
t␈↓α → concat␈↓ ∧x[mkent[first[vars];first[vals]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βh␈↓ ∧x newtbl[rest[vars];rest[vals];( )]]
␈↓ ↓H␈↓α␈↓ αX member[name[first[tbl]];vars] → newtbl[vars;vals;rest[tbl]]
␈↓ ↓H␈↓α␈↓ αX ␈↓
t␈↓α → concat[first[tbl];newtbl[vars;vals;rest[tbl]]] ]

␈↓ ↓H␈↓This␈αversion␈αof␈α
␈↓αnewtbl␈↓␈αrequires␈αmuch␈αmore␈α
computation␈αthan␈αthe␈α
alternative.␈α Its␈αadvantage␈αis␈α
that
␈↓ ↓H␈↓the␈α
"set"-ness␈α
of␈αsymbol␈α
tables␈α
is␈αmaintained.␈α
Luckily␈α
the␈α"set"␈α
property␈α
is␈αone␈α
which␈α
we␈α
need␈αnot
␈↓ ↓H␈↓depend␈αon␈αfor␈αour␈αalgorithms.␈αIndeed␈αwe␈αwill␈αfrequently␈αdepend␈αon␈αthe␈αobverse␈αproperty:␈αthat␈αthe
␈↓ ↓H␈↓previous values of variables can be found further along in the sequence.

␈↓ ↓H␈↓The␈α∂main␈α⊂point␈α∂of␈α⊂this␈α∂example␈α∂however␈α⊂is␈α∂to␈α⊂impress␈α∂on␈α∂you␈α⊂the␈α∂importance␈α⊂of␈α∂writing␈α⊂at␈α∂a
␈↓ ↓H␈↓sufficiently␈αhigh␈αlevel␈αof␈αabstraction.␈αWe␈αhave␈α
produced␈αa␈αnon-trivial␈αalgorithm␈αwhich␈αis␈αclear␈α
and
␈↓ ↓H␈↓concise.␈αIf␈αit␈αwere␈αdesirable␈αto␈αhave␈αthis␈αalgorithm␈αrunning␈αon␈αa␈αmachine␈αwe␈αcould␈αcode␈αit␈αand␈αits
␈↓ ↓H␈↓associated␈αdata␈αstructure␈αrepresentations␈αin␈αa␈αvery␈αshort␈αtime.␈α In␈αa␈αvery␈αshort␈αtime␈α␈↓↓we␈↓␈αwill␈αbe␈αable
␈↓ ↓H␈↓to run this algorithm on a LISP machine.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 43␈↓␈α
A␈α
second␈α
decision␈α
was␈α
implied␈α
in␈α
our␈α
handling␈α
of␈α
function␈α
definitions;␈α
namely␈α
we␈α∞bound␈α
the
␈↓ ↓H␈↓function␈α⊗name␈α⊗to␈α⊗a␈α⊗structure␈α⊗consisting␈α⊗of␈α⊗the␈α⊗variable␈α⊗list␈α⊗and␈α⊗the␈α⊗function␈α↔body.␈α⊗This
␈↓ ↓H␈↓representation␈α∞gives␈α∞the␈α∞expected␈α∂result␈α∞in␈α∞most␈α∞cases,␈α∞but␈α∂involves␈α∞one␈α∞of␈α∞the␈α∂more␈α∞problematic
␈↓ ↓H␈↓areas␈αof␈αprogramming␈αlanguages:␈αhow␈αdo␈αyou␈αfind␈αthe␈αbindings␈αof␈αvariables␈αwhich␈αdo␈αnot␈αappear
␈↓ ↓H␈↓in␈αthe␈αcurrent␈αvariable␈αlist?␈αFunction␈αnames␈αbelong␈αin␈αthis␈αcategory.␈αSuch␈αvariables␈αare␈αcalled␈αfree
␈↓ ↓H␈↓variables.␈α∩The␈α⊃scheme␈α∩proposed␈α∩in␈α⊃this␈α∩section␈α∩finds␈α⊃the␈α∩binding␈α∩which␈α⊃is␈α∩current␈α∩when␈α⊃the
␈↓ ↓H␈↓function␈αwas␈αapplied.␈αThe␈αprogramming␈αlanguage,␈αALGOL,␈αadvocates␈αfinding␈αthe␈αbinding␈αwhich
␈↓ ↓H␈↓was current when the function was defined.
␈↓ ↓H␈↓␈↓↓3.7␈↓ 	⊃The great mothers     81␈↓


␈↓ ↓H␈↓␈↓ ¬9␈↓↓3.7  The great mothers␈↓


␈↓ ↓H␈↓The␈αfollowing␈αproblems␈αare␈α
written␈α(intentionally)␈αwith␈αa␈αgreat␈α
deal␈αof␈αthe␈αrepresentation␈αbuilt␈α
into
␈↓ ↓H␈↓them.  They are truly ugly but should be done anyway.

␈↓ ↓H␈↓␈↓ ∧∪␈↓↓I. The Great Mother of All Functions!!! (␈↓αtgmoaf␈↓↓)

␈↓ ↓H␈↓α␈↓ αXtgmoaf[x] <=␈↓ ∧H[isindiv[x] →␈↓ ¬x[eq[x ;T] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x eq[x;NIL] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x ␈↓
t␈↓α → TRYAGAINNEXTWEEK];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];QUOTE] → second[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CAR] → car[tgmoaf[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CDR] → cdr[tgmoaf[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CONS] → cons[tgmoaf[second[x]];tgmoaf[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];ATOM] → atom[tgmoaf[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];EQ] → eq[tgmoaf[second[x]];tgmoaf[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → TRYAGAINNEXTWEEK]

␈↓ ↓H␈↓Evaluate the following:

␈↓ ↓H␈↓␈↓↓1.␈↓α tgmoaf[T]
␈↓ ↓H␈↓α␈↓↓2.␈↓α tgmoaf[A]
␈↓ ↓H␈↓α␈↓↓3.␈↓α tgmoaf[(CAR (QUOTE (A . B)))]
␈↓ ↓H␈↓α␈↓↓4.␈↓α tgmoaf[(CDR (QUOTE (A B)))]
␈↓ ↓H␈↓α␈↓↓5.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B))) (QUOTE A))]
␈↓ ↓H␈↓α␈↓↓6.␈↓α tgmoaf[(EQ (CAR (QUOTE (A . B))) A)]
␈↓ ↓H␈↓α␈↓↓7.␈↓α tgmoaf[(ATOM (CAR (QUOTE (A B))))]



␈↓ ↓H␈↓α␈↓ βI␈↓↓II. The Great Mother of All Functions Revisited!!!(␈↓αtgmoafr␈↓↓)

␈↓ ↓H␈↓α␈↓ αXtgmoafr[x] <=␈↓ ∧H[isindiv[x] →␈↓ ¬x[eq[x;T] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x eq[x;NIL] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H␈↓ ¬x ␈↓
t␈↓α → TRYAGAINNEXTWEEK];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];QUOTE] → second[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CAR] → car[tgmoafr[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CDR] → cdr[tgmoafr[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];CONS] → cons[tgmoafr[second[x]];tgmoafr[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];ATOM] → atom[tgmoafr[second[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];EQ] → eq[tgmoafr[second[x]];tgmoafr[third[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H eq[first[x];COND] → evcond[rest[x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → TRYAGAINNEXTWEEK]
␈↓ ↓H␈↓␈↓↓82  Applications␈↓ 53.7␈↓



␈↓ ↓H␈↓α␈↓ αXevcond[x] <=␈↓ ∧H[tgmoafr[first[first[x]]] → tgmoafr[second[first[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → evcond[rest[x]] ]

␈↓ ↓H␈↓Evaluate the following:

␈↓ ↓H␈↓␈↓↓1.␈↓α tgmoafr[T]
␈↓ ↓H␈↓α␈↓↓2.␈↓α tgmoafr[(CDR (QUOTE (A B)))]
␈↓ ↓H␈↓α␈↓↓3.␈↓α tgmoafr[(EQ (CAR (QUOTE (A . B))) (QUOTE A))]
␈↓ ↓H␈↓α␈↓↓4.␈↓α tgmoafr[(COND ((EQ (CAR (QUOTE (A . B))) (QUOTE A))(QUOTE FOO)))]
␈↓ ↓H␈↓α␈↓↓5.␈↓α tgmoafr[(COND ((ATOM (QUOTE (A)))(QUOTE FOO)) (T(QUOTE BAZ)))]


␈↓ ↓H␈↓α␈↓ ∧cComing soon: ␈↓↓Son of Great Mother !!␈↓α



␈↓ ↓H␈↓␈↓ ¬G␈↓↓3.8  Another Respite␈↓


␈↓ ↓H␈↓We␈αhave␈α
again␈αreached␈αa␈α
point␈αwhere␈αa␈α
certain␈αamount␈αof␈α
reflection␈αwould␈αbe␈α
good␈αfor␈α
the␈αsoul.
␈↓ ↓H␈↓Though␈α
this␈α
is␈α
not␈α
a␈α
programming␈α
manual␈α
we␈α
would␈α
be␈α
remiss␈α
if␈α
we␈α
did␈α
not␈α
attempt␈α
to␈αanalyze␈α
the
␈↓ ↓H␈↓style which we have tried to exercise when writing programs.

␈↓ ↓H␈↓1.␈αWrite␈αthe␈αalgorithm␈α
in␈αan␈αabstract␈αsetting;␈α
do␈αnot␈αmuddle␈αthe␈α
abstract␈αalgorithm␈αwith␈αthe␈α
chosen
␈↓ ↓H␈↓representation.␈α∞ If␈α∂you␈α∞follow␈α∞this␈α∂dictum␈α∞your␈α∞LISP␈α∂programs␈α∞will␈α∞never␈α∂use␈α∞␈↓αcar,␈α∞cdr,␈α∂cons␈↓,␈α∞and
␈↓ ↓H␈↓␈↓αatom␈↓.␈α∀ All␈α∪instances␈α∀of␈α∀these␈α∪LISP␈α∀primitives␈α∪will␈α∀be␈α∀banished␈α∪to␈α∀small␈α∀subfunctions␈α∪which
␈↓ ↓H␈↓manipulate representations.

␈↓ ↓H␈↓2.␈α∀When␈α∀writing␈α∃the␈α∀abstract␈α∀program,␈α∃do␈α∀not␈α∀be␈α∃afraid␈α∀to␈α∀cast␈α∃off␈α∀difficult␈α∀parts␈α∃of␈α∀the
␈↓ ↓H␈↓implementation␈α∞to␈α∞subfunctions.␈α∞Remember␈α∞that␈α∞if␈α
you␈α∞have␈α∞trouble␈α∞keeping␈α∞the␈α∞details␈α∞in␈α
mind
␈↓ ↓H␈↓when␈α␈↓↓writing␈↓␈αthe␈α
program,␈αthen␈αthe␈αconfusion␈α
involved␈αin␈α␈↓↓reading␈↓␈αthe␈α
program␈αat␈αsome␈αlater␈α
time
␈↓ ↓H␈↓will␈α∀be␈α∀overwhelming.␈α∃Once␈α∀you␈α∀have␈α∃convinced␈α∀yourself␈α∀of␈α∃the␈α∀correctness␈α∀of␈α∃the␈α∀current
␈↓ ↓H␈↓composition,␈α∂then␈α∂worry␈α∂about␈α∂the␈α∂construction␈α∂of␈α∂the␈α∂subfunctions.␈α∂Seldom␈α∂does␈α∂the␈α⊂process␈α∂of
␈↓ ↓H␈↓composing␈α∀a␈α∀program␈α∀flow␈α∀so␈α∀gently␈α∀from␈α∀top-level␈α∀to␈α∀specific␈α∀representation.␈α∀Only␈α∀the␈α∪toy
␈↓ ↓H␈↓programs␈α∞are␈α∂easy;␈α∞the␈α∂construction␈α∞of␈α∂the␈α∞practical␈α∞program␈α∂will␈α∞be␈α∂confusing,␈α∞and␈α∂will␈α∞require
␈↓ ↓H␈↓much rethinking.  But bring as much structure as you can to the process.

␈↓ ↓H␈↓3.␈α⊂From␈α⊂the␈α⊂other␈α⊂side␈α⊂of␈α⊂the␈α⊂question,␈α⊂don't␈α⊂be␈α⊂afraid␈α⊂to␈α⊂look␈α⊂at␈α⊂specific␈α⊃implementations,␈α⊂or
␈↓ ↓H␈↓specific␈α⊗data-structure␈α⊗representations␈α↔before␈α⊗you␈α⊗begin␈α⊗to␈α↔write.␈α⊗There␈α⊗is␈α↔something␈α⊗quite
␈↓ ↓H␈↓comforting␈α⊂about␈α⊂a␈α⊂"real"␈α⊂data␈α⊂structure.␈α⊂Essentially␈α⊂data␈α⊂structures␈α⊂are␈α⊂static␈α⊂objects␈α⊃␈↓π 44␈↓,␈α⊂while
␈↓ ↓H␈↓programs␈α∂are␈α∂dynamic␈α∂objects.␈α∂A␈α∂close␈α∂look␈α∂at␈α∂a␈α∂possible␈α∂representation␈α∂may␈α∂get␈α∂you␈α⊂a␈α∂starting
␈↓ ↓H␈↓point␈α
and␈α
as␈α
you␈α
write␈α
the␈α
program␈α
it␈α
will␈α
become␈α
clear␈α
when␈α
you␈α
are␈α
depending␈α
on␈α
the␈αspecific
␈↓ ↓H␈↓representation and when you are just using properties of an abstract data structure.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 44␈↓ At least within the program presently being constructed.
␈↓ ↓H␈↓␈↓↓3.8␈↓ 	.Another Respite     83␈↓


␈↓ ↓H␈↓Perhaps␈α
the␈αmore␈α
practical␈α
reader␈αis␈α
ovecome␈α
by␈αthe␈α
inefficiencies␈α
inherent␈αin␈α
these␈αproposals.␈α
Two
␈↓ ↓H␈↓answers:␈α
first,␈α
"inefficiency"␈α
is␈αa␈α
very␈α
etherial␈α
concept.␈α Like␈α
"structured␈α
programming",␈α
it␈αis␈α
difficult
␈↓ ↓H␈↓to␈α
define␈αbut␈α
recognizable␈αwhen␈α
it␈αoccurs.␈α
 Hardware␈α
and␈αsoftware␈α
development␈αhas␈α
been␈αsuch␈α
that
␈↓ ↓H␈↓last␈α∞year's␈α∞"inefficiency"␈α
is␈α∞this␈α∞year's␈α∞␈↓αpasse␈↓␈α
programming␈α∞trick.␈α∞But␈α∞even␈α
at␈α∞a␈α∞more␈α∞topical␈α
level,
␈↓ ↓H␈↓much␈α∀of␈α∀what␈α∃seems␈α∀inefficent␈α∀can␈α∃now␈α∀be␈α∀straightened␈α∀out␈α∃by␈α∀a␈α∀compiler␈α∃(see␈α∀Chapter ).
␈↓ ↓H␈↓Frequently␈α∂compilers␈α∂can␈α∂do␈α⊂very␈α∂clever␈α∂optimizations␈α∂to␈α∂generate␈α⊂efficient␈α∂code.␈α∂ It␈α∂is␈α⊂better␈α∂to
␈↓ ↓H␈↓leave the cleverness to the compiler, and the clarity to the programmer.

␈↓ ↓H␈↓Second,␈α⊗the␈α⊗problems␈α↔in␈α⊗programming␈α⊗are␈α↔not␈α⊗those␈α⊗of␈α↔efficiency.␈α⊗They␈α⊗are␈α↔problems␈α⊗of
␈↓ ↓H␈↓␈↓↓correctness␈↓.␈α∞How␈α∞do␈α∂you␈α∞write␈α∞a␈α∂program␈α∞which␈α∞works?␈α∞ Until␈α∂practical␈α∞tools␈α∞are␈α∂developed␈α∞for
␈↓ ↓H␈↓proving␈α
correctness␈α
it␈α
is␈αup␈α
to␈α
the␈α
programmer␈α
to␈αcertify␈α
his␈α
programs.␈α
Any␈α
methodology␈αwhich␈α
can
␈↓ ↓H␈↓aid␈α
the␈α
programmer␈α
will␈αbe␈α
most␈α
welcome.␈α
 Clearly,␈αthe␈α
closer␈α
you␈α
can␈αwrite␈α
the␈α
program␈α
to␈αyour
␈↓ ↓H␈↓intuition,␈αthe␈αless␈α
chance␈αthere␈αis␈αfor␈α
error.␈αThis␈αwas␈αone␈α
of␈αthe␈αreasons␈αfor␈α
developing␈αhigh-level
␈↓ ↓H␈↓languages.␈α∂But␈α⊂do␈α∂not␈α∂forget␈α⊂that␈α∂the␈α∂original␈α⊂motivation␈α∂for␈α∂such␈α⊂languages␈α∂was␈α⊂a␈α∂convenient
␈↓ ↓H␈↓notation␈α∞for␈α∞expressing␈α
numerical␈α∞problems,␈α∞that␈α
is,␈α∞for␈α∞writing␈α
programs␈α∞to␈α∞express␈α∞ideas␈α
which
␈↓ ↓H␈↓have␈α∞already␈α∞had␈α∂their␈α∞juices␈α∞extracted␈α∂as␈α∞mathematical␈α∞formulas.␈α∂ With␈α∞data␈α∞structures,␈α∂we␈α∞are
␈↓ ↓H␈↓able␈αto␈αformalize␈α
a␈αbroader␈αrange␈αof␈α
domains,␈αexpressing␈αour␈α
ideas␈αas␈αdata␈αstructure␈α
manipulations
␈↓ ↓H␈↓rather than as numerical relationships.

␈↓ ↓H␈↓What␈α
kinds␈α∞of␈α
errors␈α
are␈α∞prevelant␈α
in␈α
data␈α∞structure␈α
programming?␈α
At␈α∞least␈α
two␈α
kinds:␈α∞errors␈α
of
␈↓ ↓H␈↓omission -- misunderstanding␈α∩of␈α⊃the␈α∩basic␈α⊃algorithm;␈α∩and␈α⊃errors␈α∩of␈α⊃commission -- errors␈α∩due␈α⊃to
␈↓ ↓H␈↓misapplied cleverness in attempting to be efficient.

␈↓ ↓H␈↓The␈α∞occurrences␈α∞of␈α
errors␈α∞of␈α∞omission␈α∞can␈α
be␈α∞lessened␈α∞by␈α
presenting␈α∞the␈α∞user␈α∞with␈α
programming
␈↓ ↓H␈↓constructs␈α
which␈α
are␈αclose␈α
to␈α
the␈αintuited␈α
algorithm.␈α
 Such␈αconstructs␈α
include␈α
control␈αstructures,␈α
data
␈↓ ↓H␈↓structures, and representations for operations.

␈↓ ↓H␈↓Errors␈α∂of␈α∂commission␈α∂comprise␈α∂the␈α∂great␈α∂majority␈α∂of␈α∂the␈α∂present␈α∂day␈α∂headaches.␈α∂It␈α∂is␈α⊂here␈α∂that
␈↓ ↓H␈↓programming␈α␈↓↓style␈↓␈αcan␈αbe␈αbeneficial:␈αkeep␈αthe␈αrepresentation␈αof␈αthe␈αdata␈αstructures␈αaway␈αfrom␈αthe
␈↓ ↓H␈↓description␈α∃of␈α∃the␈α∃algorithm;␈α∃write␈α∃concise␈α∃abstract␈α∃programs,␈α∃passing␈α∃off␈α⊗responsibilities␈α∃to
␈↓ ↓H␈↓subfunctions.␈α⊂Whenever␈α⊂a␈α⊂definition␈α⊂of␈α⊂"structured␈α⊂programming"␈α⊂is␈α⊂arrived␈α⊂at,␈α⊂this␈α⊃advice␈α⊂on
␈↓ ↓H␈↓programming style should be included.

␈↓ ↓H␈↓Before␈α
closing␈α
this␈α
discussion␈α
on␈αthe␈α
philosphy␈α
of␈α
LISP␈α
programming,␈αwe␈α
can't␈α
but␈α
note␈α
that␈αthe
␈↓ ↓H␈↓preceding␈αsection,␈α␈↓↓The␈αGreat␈αMothers␈↓,␈αcompletely␈αignored␈αour␈αgood␈αadvice.␈αThis␈αwould␈αbe␈α
a␈αgood
␈↓ ↓H␈↓time␈α↔for␈α↔the␈α↔interested␈α↔reader␈α↔to␈α↔abstract␈α↔the␈α↔␈↓αtgmoaf␈↓␈α↔algorithm␈α↔from␈α↔the␈α↔particular␈α⊗data
␈↓ ↓H␈↓representation. This detective work will be most rewarding.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Write an abstract version of ␈↓αtgmoaf␈↓.
␈↓ ↓H␈↓␈↓↓84  Applications␈↓ 53.9␈↓


␈↓ ↓H␈↓␈↓ ∧↑␈↓↓3.9  Proving properties of programs␈↓


␈↓ ↓H␈↓People␈α
are␈α
becoming␈αincreasingly␈α
aware␈α
of␈αthe␈α
importance␈α
of␈αgiving␈α
convincing␈α
arguments␈αfor␈α
such
␈↓ ↓H␈↓things␈αas␈αthe␈αcorrectness␈αor␈αequivalence␈αof␈αprograms.␈αThese␈αare␈αboth␈αvery␈αdifficult␈αenterprises.␈αWe
␈↓ ↓H␈↓will␈αonly␈αsketch␈αa␈α
proof␈αof␈αa␈αsimple␈αproperty␈α
of␈αtwo␈αprograms␈αand␈α
leave␈αothers␈αas␈αproblems␈αfor␈α
the
␈↓ ↓H␈↓interested␈αreader.␈α How␈αdo␈αyou␈αgo␈αabout␈αproving␈αproperties␈αof␈αprograms?␈α In␈αSection 2.9␈αwe␈αnoted
␈↓ ↓H␈↓certain␈α⊃benefits␈α⊃of␈α⊃defining␈α⊃sets␈α⊃using␈α⊃inductive␈α⊃definitions.␈α⊃First,␈α⊃there␈α⊃was␈α⊃a␈α⊃natural␈α⊃way␈α⊃of
␈↓ ↓H␈↓thinking␈αabout␈αthe␈αconstruction␈αof␈αan␈αalgorithm␈αover␈αthat␈αset.␈αWe␈αhave␈αexploited␈αthat␈αobservation
␈↓ ↓H␈↓in␈α∞our␈α∞study␈α∞of␈α∞LISP␈α∞programming.␈α∞What␈α∞we␈α∞need␈α∞recall␈α∞here␈α∞is␈α∞the␈α∞observation␈α∂that␈α∞inductive
␈↓ ↓H␈↓style␈α
proofs␈α
(see␈α
␈↓	PRF␈↓␈α
on␈α
page 45)␈α
are␈α
valid␈α
forms␈α
of␈α
reasoning␈α
over␈α
such␈α
domains.␈α
Since␈α
we␈αin␈α
fact
␈↓ ↓H␈↓defined␈αour␈αdata␈αstructure␈αdomains␈αin␈αan␈αinductive␈αmanner,␈αit␈αseems␈αnatural␈αto␈αlook␈αfor␈αinductive
␈↓ ↓H␈↓arguments␈αwhen␈α
proving␈αproperties␈αof␈α
programs.␈αThis␈αis␈α
indeed␈αwhat␈αwe␈α
do;␈αwe␈αperform␈α
induction
␈↓ ↓H␈↓on the structure of the elements in the data domain.

␈↓ ↓H␈↓For␈αexample,␈αusing␈αthe␈αdefinition␈αof␈α␈↓αappend␈↓␈αgiven␈αon␈αpage 49␈αand␈αthe␈αdefinition␈αof␈α␈↓αreverse␈↓␈αgiven
␈↓ ↓H␈↓on page 50, we wish to show that:

␈↓ ↓H␈↓␈↓ ∧α␈↓αappend[reverse[y];reverse[x]] = reverse[append[x;y]]␈↓, 

␈↓ ↓H␈↓for any lists, ␈↓αx␈↓, and ␈↓αy␈↓. The induction will be on the structure of ␈↓αx␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓↓Basis␈↓: ␈↓αx␈↓ is ␈↓α( )␈↓.
␈↓ ↓H␈↓␈↓ αλWe must thus show: ␈↓αappend[reverse[y];( )] = reverse[append[( );y]]␈↓
␈↓ ↓H␈↓␈↓ αλBut: ␈↓αreverse[append[( );y]] = reverse[y]␈↓  by the def. of ␈↓αappend␈↓.
␈↓ ↓H␈↓␈↓ αλWe now establish the stronger result:  ␈↓αappend[z;( )] = z␈↓ ␈↓π 45␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ αX␈↓↓Basis:␈↓ ␈↓αz␈↓ is ␈↓α( )␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓ αXShow ␈↓αappend[( );( )] = ( )␈↓. Easy.
␈↓ ↓H␈↓␈↓ αλ␈↓ αX␈↓↓Induction step:␈↓ Assume the lemma for lists, ␈↓αz␈↓, of length n;
␈↓ ↓H␈↓␈↓ αλ␈↓ αXProve: ␈↓αappend[concat[x;z];( )] = concat[x;z]␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓ αXSince ␈↓αconcat[...]␈↓ is not ␈↓α( )␈↓, then applying the definition of ␈↓αappend␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ αXsays we must prove: ␈↓αconcat[x;append[z;( )]] = concat[x;z]␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓ αXBut our induction hypothesis is applicable since ␈↓αz␈↓ is shorter than ␈↓αconcat[x;z]␈↓.
␈↓ ↓H␈↓␈↓ αλ␈↓ αXOur result follows.
␈↓ ↓H␈↓␈↓ αλSo the Basis for our main result is established.










␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 45␈↓ In the following proof several intermediate steps have been ommitted.
␈↓ ↓H␈↓␈↓↓3.9␈↓ π\Proving properties of programs     85␈↓


␈↓ ↓H␈↓␈↓ αλ␈↓↓Induction step:␈↓ Assume the result for lists, ␈↓αz␈↓, of length n;
␈↓ ↓H␈↓␈↓ αλProve:
␈↓ ↓H␈↓␈↓ αλ␈↓ α`   (1)   ␈↓αappend[reverse[y];reverse[concat[x;z]]] = reverse[append[concat[x;z];y]]␈↓
␈↓ ↓H␈↓␈↓ αλ Using the definition of ␈↓αreverse␈↓ on the LHS of (1) gives:
␈↓ ↓H␈↓␈↓ αλ␈↓ ∧¬   (2)   ␈↓αappend[reverse[y];append[reverse[z];list[x]]]␈↓.
␈↓ ↓H␈↓␈↓ αλ Using the definition of ␈↓αappend␈↓ on the RHS of (1) gives:
␈↓ ↓H␈↓␈↓ αλ␈↓ ∧b   (3)   ␈↓αreverse[concat[x;append[z;y]]].␈↓
␈↓ ↓H␈↓␈↓ αλ Using the definition of ␈↓αreverse␈↓ on (3) gives:
␈↓ ↓H␈↓␈↓ αλ␈↓ ∧:   (4)   ␈↓αappend[reverse[append[z;y]];list[x]].␈↓
␈↓ ↓H␈↓␈↓ αλ Using our induction hypothesis on (4) gives:
␈↓ ↓H␈↓␈↓ αλ␈↓ ∧λ   (5)   ␈↓αappend[append[reverse[y];reverse[z]];list[x]]␈↓
␈↓ ↓H␈↓␈↓ αλ Thus we must establish that    (2) = (5).
␈↓ ↓H␈↓␈↓ αλ But this is just an instance of the associativity of ␈↓αappend␈↓:
␈↓ ↓H␈↓␈↓ αλ␈↓ αz␈↓αappend[x;append[y;z]] = append[append[x;y];z].␈↓  (See problem I, below.)

␈↓ ↓H␈↓The␈αstructure␈αof␈αthe␈αproof␈αis␈αanalogous␈αto␈αproofs␈αby␈αmathematical␈αinduction␈αin␈αelementary␈α
number
␈↓ ↓H␈↓theory.␈αThe␈αability␈αto␈αperform␈αsuch␈αproofs␈αis␈αa␈αdirect␈αconsequence␈αof␈αour␈αcareful␈αdefinition␈αof␈αdata
␈↓ ↓H␈↓structures.␈α Examination␈αof␈αthe␈αproof␈αwill␈αshow␈αthat␈αthere␈αis␈αa␈αclose␈αrelationship␈αbetween␈αwhat␈αwe
␈↓ ↓H␈↓are␈α∪inducting␈α∪on␈α∪in␈α∪the␈α∩proof␈α∪and␈α∪what␈α∪we␈α∪are␈α∩recurring␈α∪on␈α∪during␈α∪the␈α∪evaluation␈α∪of␈α∩the
␈↓ ↓H␈↓expressions.␈αA␈αprogram␈αwritten␈αby␈αBoyer␈αand␈αMoore␈αhas␈αbeen␈αreasonably␈αsuccessful␈αin␈αgenerating
␈↓ ↓H␈↓proofs like the above by exploiting this relationship.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Prove the associativity of ␈↓αappend␈↓.

␈↓ ↓H␈↓II␈α
Analysis␈αof␈α
the␈αabove␈α
proof␈αshows␈α
frequent␈αuse␈α
of␈αother␈α
results␈αfor␈α
LISP␈αfunctions.␈α
Fill␈α
in␈αthe
␈↓ ↓H␈↓details. Investigate the possibility of formalizing this proof, showing what axioms are needed.

␈↓ ↓H␈↓III Show the equivalence of ␈↓αfact␈↓ (page 46) and ␈↓αfact␈↓β1␈↓ (page 48).

␈↓ ↓H␈↓IV Show the equivalence of ␈↓αlength␈↓ and ␈↓αlength␈↓β1␈↓ (page 48).

␈↓ ↓H␈↓V Using the definition of ␈↓αreverse␈↓, given on page 49, prove:

␈↓ ↓H␈↓␈↓ ¬A␈↓αreverse[reverse[x]] = x␈↓. 
␈↓ ↓H␈↓␈↓↓86  Evaluation␈↓ @4.␈↓


␈↓ ↓H␈↓␈↓ ¬⎇␈↓↓SECTION 4

␈↓ ↓H␈↓↓␈↓ ∧6EVALUATION OF LISP EXPRESSIONS␈↓




␈↓ ↓H␈↓␈↓ ∧λ"...␈αI␈αalways␈αworked␈αwith␈αprogramming␈αlanguages␈αbecause␈αit␈αseemed␈αto␈αme
␈↓ ↓H␈↓␈↓ ∧λthat␈α∪until␈α∪you␈α∪could␈α∪understand␈α∪those,␈α∪you␈α∪really␈α∪couldn't␈α∪understand
␈↓ ↓H␈↓␈↓ ∧λcomputers.␈αUnderstanding␈αthem␈αdoesn't␈αreally␈αmean␈αonly␈αbeing␈αable␈αto␈αuse
␈↓ ↓H␈↓␈↓ ∧λthem.  A lot of people can use them without understanding them. ..."

␈↓ ↓H␈↓␈↓ λbChristopher Strachey[Str 74] 



␈↓ ↓H␈↓␈↓ ¬←␈↓↓4.1  Introduction␈↓


␈↓ ↓H␈↓In␈αthe␈αprevious␈αsections␈αof␈αthis␈αtext␈αwe␈αhave␈αtalked␈αabout␈αsome␈αof␈αthe␈αschemes␈αfor␈αevaluation.␈α We
␈↓ ↓H␈↓have␈α
done␈α
so␈α
rather␈α
informally␈α
for␈α
LISP;␈α∞we␈α
have␈α
been␈α
more␈α
precise␈α
about␈α
evaluation␈α∞of␈α
simple
␈↓ ↓H␈↓arithmetic␈αexpressions.␈αSection 3.6␈αdiscusses␈αthis␈αin␈αsome␈αdetail.␈α We␈αshall␈αnow␈αlook␈αmore␈αclosely␈αat
␈↓ ↓H␈↓the␈α∞informal␈α∞process␈α∞which␈α∞we␈α∞have␈α∞been␈α∂using␈α∞in␈α∞the␈α∞evaluation␈α∞of␈α∞LISP␈α∞expressions.␈α∂ This␈α∞is
␈↓ ↓H␈↓motivated by at least two desires.

␈↓ ↓H␈↓First,␈αwe␈αwant␈αto␈αrun␈αour␈αLISP␈αprograms␈αon␈αa␈αmachine.␈α To␈αdo␈αso␈αrequires␈αthe␈αimplementation␈αof
␈↓ ↓H␈↓some␈α∞kind␈α∂of␈α∞translator␈α∞to␈α∂turn␈α∞LISP␈α∂programs␈α∞into␈α∞instructions␈α∂which␈α∞can␈α∞be␈α∂carried␈α∞out␈α∂by␈α∞a
␈↓ ↓H␈↓conventional␈α∪machine.␈α∩ We␈α∪will␈α∩be␈α∪interested␈α∩in␈α∪the␈α∩structure␈α∪of␈α∩such␈α∪implementations.␈α∩ Any
␈↓ ↓H␈↓implementation␈α∀of␈α∃LISP␈α∀must␈α∀be␈α∃grounded␈α∀on␈α∃a␈α∀precise,␈α∀and␈α∃clear␈α∀understanding␈α∃of␈α∀what
␈↓ ↓H␈↓LISP-evaluation␈α↔entails.␈α↔Indeed,␈α↔a␈α↔deep␈α_understanding␈α↔of␈α↔evaluation␈α↔is␈α↔a␈α_prerequisite␈α↔for
␈↓ ↓H␈↓implementation␈α
of␈α∞␈↓↓any␈↓␈α
language.␈α∞ The␈α
question␈α∞of␈α
evaluation␈α∞cannot␈α
be␈α∞sidestepped␈α
by␈α∞basing␈α
a
␈↓ ↓H␈↓language␈α∪on␈α∩a␈α∪compiler.␈α∪A␈α∩compiler␈α∪must␈α∩produce␈α∪code␈α∪which␈α∩when␈α∪executed,␈α∪simulates␈α∩the
␈↓ ↓H␈↓evaluation process. There is no escape.

␈↓ ↓H␈↓Our␈α∪second␈α∀reason␈α∪for␈α∀pursuing␈α∪evaluation␈α∀involves␈α∪the␈α∀question␈α∪of␈α∀programming␈α∪language
␈↓ ↓H␈↓specification.␈α
This␈α
title␈α
covers␈α
a␈α
multitude␈α
of␈α
sins.␈α
 At␈α
a␈α
practical␈α
level␈α
we␈α
want␈α
a␈α
clean,␈α
machine
␈↓ ↓H␈↓independent,␈α∞"self-evident"␈α∞description␈α∞of␈α∞a␈α∞language␈α∞specification,␈α∞so␈α∞that␈α∞the␈α∞agony␈α∞involved␈α∞in
␈↓ ↓H␈↓implementing␈αthe␈αdesign␈αcan␈αbe␈αminimized.␈α At␈αa␈αmore␈αabstract␈αlevel,␈αwe␈αshould␈αtry␈αto␈αunderstand
␈↓ ↓H␈↓just␈α
what␈α
␈↓↓is␈↓␈α
specified␈α
when␈α
we␈α
design␈α
a␈αlanguage.␈α
Are␈α
we␈α
specifying␈α
a␈α
single␈α
machine,␈α
a␈α
class␈αof
␈↓ ↓H␈↓machines,␈α
a␈αclass␈α
of␈α
mathematical␈αfunctions.␈α
Just␈α
what␈αis␈α
a␈α
language?␈α The␈α
syntactic␈αspecification␈α
of
␈↓ ↓H␈↓languages␈α∞is␈α∞reasonably␈α∂well␈α∞established,␈α∞but␈α∂syntax␈α∞is␈α∞only␈α∂the␈α∞tip␈α∞of␈α∂the␈α∞iceberg.␈α∞Our␈α∂study␈α∞of
␈↓ ↓H␈↓LISP will address itself to the deeper problems of semantics, or meaning, of languages.

␈↓ ↓H␈↓Before␈αwe␈αaddress␈αthe␈αdirect␈αquestion␈αof␈αLISP␈αevaluation,␈αwe␈αshould␈αperhaps␈αwonder␈αaloud␈αabout
␈↓ ↓H␈↓the␈αefficacy␈αof␈αstudying␈αlanguages␈αin␈αthe␈αdetail␈αwhich␈αwe␈αare␈αproposing.␈α As␈αcomputer␈αscientists␈αwe
␈↓ ↓H␈↓␈↓↓4.1␈↓ 	↑Introduction     87␈↓


␈↓ ↓H␈↓should␈αbe␈αcurious␈αabout␈αthe␈αstructure␈αof␈αprogramming␈αlanguages␈αbecause␈αwe␈αmust␈αunderstand␈αour
␈↓ ↓H␈↓tools␈α∞-- our␈α∞programming languages.␈α∂People␈α∞who␈α∞simply␈α∂wish␈α∞to␈α∞␈↓↓use␈↓␈α∂computers␈α∞as␈α∞tools␈α∂need␈α∞not
␈↓ ↓H␈↓care␈α∩about␈α∩the␈α∩structure␈α∩of␈α∩languages.␈α∩Indeed␈α∩they␈α∩usually␈α∩couldn't␈α∩care␈α∩less␈α∩about␈α∩the␈α∩inner
␈↓ ↓H␈↓workings␈α∞of␈α∞the␈α
language;␈α∞they␈α∞only␈α
want␈α∞languages␈α∞in␈α
which␈α∞they␈α∞can␈α
state␈α∞their␈α∞problems␈α∞in␈α
a
␈↓ ↓H␈↓reasonably␈αnatural␈αmanner.␈α They␈αwant␈αtheir␈αprograms␈αto␈αrun␈αand␈αget␈αresults.␈αThey␈αare␈αinterested
␈↓ ↓H␈↓in␈α⊂the␈α⊃output␈α⊂and␈α⊂seldom␈α⊃are␈α⊂interested␈α⊂in␈α⊃the␈α⊂detailed␈α⊂process␈α⊃of␈α⊂computation.␈α⊂ For␈α⊃a␈α⊂simple
␈↓ ↓H␈↓analogy,␈αconsider␈αthe␈αfield␈αof␈αmathematics.␈α The␈αpracticing␈αmathematician␈αuses␈αhis␈αtools␈α
-- proofs --
␈↓ ↓H␈↓in␈α
a␈α
similar␈α
manner␈αto␈α
the␈α
person␈α
interested␈α
in␈αcomputer␈α
applications.␈α
 He␈α
seldom␈α
needs␈αto␈α
examine
␈↓ ↓H␈↓questions␈α
like␈α
"what␈α
is␈α
a␈α
proof?"␈α
He␈α
does␈αnot␈α
analyze␈α
his␈α
tools.␈α
 However,␈α
it␈α
must␈α
be␈α
pointed␈αout
␈↓ ↓H␈↓that␈αnot␈α
so␈αmany␈α
years␈αago␈α
such␈αquestions␈αindeed␈α
were␈αraised,␈α
and␈αfor␈α
good␈αreason.␈α Some␈α
common
␈↓ ↓H␈↓forms of reasoning were shown to lead to contradictions unless care was taken.

␈↓ ↓H␈↓Our␈α
position␈α
is␈α
more␈α
like␈α
that␈α
of␈α
the␈α
foundations␈α
of␈α
mathematics;␈α
there␈α
the␈α
tools␈α
of␈α
mathematics␈α
␈↓↓are␈↓
␈↓ ↓H␈↓studied␈α∃and␈α∃held␈α⊗up␈α∃to␈α∃the␈α⊗light.␈α∃Mathematics␈α∃has␈α∃flourished␈α⊗because␈α∃of␈α∃it.␈α⊗Though␈α∃our
␈↓ ↓H␈↓expectations␈α⊂are␈α∂not␈α⊂quite␈α∂that␈α⊂presumptuous,␈α⊂we␈α∂␈↓↓do␈↓␈α⊂expect␈α∂that␈α⊂programming␈α⊂language␈α∂design
␈↓ ↓H␈↓cannot help but be improved.

␈↓ ↓H␈↓Our␈α∞study␈α
of␈α∞language␈α
implementation␈α∞will␈α
proceed␈α∞from␈α
the␈α∞abstract␈α
to␈α∞the␈α
concrete.␈α∞Each␈α
level
␈↓ ↓H␈↓will␈αintimately␈αinvolve␈αthe␈αstudy␈αof␈αdata␈αstructures.␈α The␈αcurrent␈αchapter␈αwill␈αbe␈αthe␈αmost␈αabstract,
␈↓ ↓H␈↓building␈α
a␈α
precise␈α
high-level␈α
description␈α
of␈α
an␈α
evaluation␈α
scheme␈α
for␈α
LISP.␈α
In␈α
fact,␈α
the␈α
discussion␈α
is
␈↓ ↓H␈↓much␈α
more␈α
general␈α
than␈α
that␈α
of␈α
LISP;␈α
the␈αtext␈α
addresses␈α
itself␈α
to␈α
problem␈α
areas␈α
in␈α
the␈α
design␈αof
␈↓ ↓H␈↓any␈α
reasonably␈αsophisticated␈α
language.␈αIn␈α
subsequent␈αchapters␈α
we␈αprobe␈α
beneath␈αthe␈α
surface␈αof␈α
this
␈↓ ↓H␈↓high-level description and discuss common ways of implementing the necessary data structures.

␈↓ ↓H␈↓But␈α
how␈α
can␈α
we␈α
begin␈α
to␈α
understand␈α
LISP␈α
evaluation?␈α
 In␈α
Section 3.6␈α
we␈α
made␈α
a␈αbeginning,␈α
giving
␈↓ ↓H␈↓an␈αalgorithm␈α
for␈αa␈α
subset␈αof␈αthe␈α
computations␈αexpressible␈α
in␈αLISP.␈α
 This␈αsubset␈αcovered␈α
evaluation
␈↓ ↓H␈↓of␈α∩some␈α∩simple␈α∩arithmetic␈α∩expressions.␈α∩ From␈α∩our␈α∩earliest␈α∩grade␈α∩school␈α∩days␈α∩we␈α∩have␈α∩had␈α⊃to
␈↓ ↓H␈↓evaluate␈α⊂simple␈α⊂arithmetic␈α⊃expressions.␈α⊂Later,␈α⊂in␈α⊂algebra␈α⊃we␈α⊂managed␈α⊂to␈α⊂cope␈α⊃with␈α⊂expressions
␈↓ ↓H␈↓involving␈α∀function␈α∀application.␈α∀Most␈α∀of␈α∀us␈α∀survived␈α∀the␈α∀experience.␈α∀We␈α∀should␈α∀now␈α∃try␈α∀to
␈↓ ↓H␈↓understand␈α∞the␈α∞processes␈α∞we␈α
used␈α∞in␈α∞these␈α∞simple␈α∞arithmetic␈α
cases,␈α∞doing␈α∞our␈α∞examination␈α∞at␈α
the
␈↓ ↓H␈↓most␈α∂mechanical␈α∂level.␈α∂ The␈α∂basic␈α∂intent␈α⊂of␈α∂the␈α∂algorithm␈α∂is␈α∂fixed:␈α∂evaluate␈α∂the␈α⊂expression;␈α∂but
␈↓ ↓H␈↓within␈αthat␈αgeneral␈αconstraint␈α
we␈αoften␈αhave␈αseveral␈α
distinct␈αalternative␈αmethods.␈α Those␈α
places␈αat
␈↓ ↓H␈↓which␈αwe␈αhave␈αchoice␈αshould␈αbe␈αremembered.␈αWe␈αwill␈αmake␈αreasonable␈αchoices␈αso␈αthat␈αthe␈αprocess
␈↓ ↓H␈↓becomes␈αdeterministic␈αand␈αproceed.␈αLater,␈αwe␈αshould␈αreflect␈αon␈αwhat␈αeffect␈αour␈αchoices␈αhad␈αon␈αthe
␈↓ ↓H␈↓resulting␈α⊃scheme.␈α⊂ For␈α⊃example,␈α⊂recall␈α⊃the␈α⊂discussion␈α⊃of␈α⊂the␈α⊃representation␈α⊂of␈α⊃symbol␈α⊃tables␈α⊂on
␈↓ ↓H␈↓page 80.␈α
We␈α∞had␈α
several␈α∞options,␈α
but␈α
picked␈α∞one␈α
which␈α∞seemed␈α
to␈α
satisfy␈α∞our␈α
intuitions␈α∞and␈α
was
␈↓ ↓H␈↓reasonably␈αefficient.␈α
But␈αwe␈α
should␈αsubject␈α
that␈αdecision␈α
to␈αclose␈α
scrutiny:␈αdoes␈α
it␈αreally␈α
fulfill␈αour
␈↓ ↓H␈↓expectations␈α∩and␈α∩is␈α∩it␈α∪efficient?␈α∩In␈α∩absence␈α∩of␈α∪absolute␈α∩standards,␈α∩these␈α∩questions␈α∪are␈α∩usually
␈↓ ↓H␈↓answered by examining the behavior of the algorithm.

␈↓ ↓H␈↓The␈α
first␈α
thing␈α
to␈α
note␈α
in␈α
examining␈α
simple␈α
arithmetic␈α
examples␈α
is␈α
that␈α
␈↓↓nothing␈↓␈α
is␈α
really␈α
said␈α
about
␈↓ ↓H␈↓the␈α∀process␈α∀of␈α∪evaluation.␈α∀ When␈α∀asked␈α∪to␈α∀evaluate␈α∀␈↓α(2*3) + (5*6)␈↓␈α∪we␈α∀never␈α∀specified␈α∪which
␈↓ ↓H␈↓summand␈αwas␈αto␈α
be␈αevaluated␈αfirst.␈α Indeed␈α
it␈αdidn't␈αmatter␈αhere.␈α
␈↓α6 + (5*6)␈↓␈αor␈α␈↓α(2*3) + 30␈↓␈αboth␈α
end
␈↓ ↓H␈↓up␈α␈↓α36␈↓.␈α Does␈αit␈α␈↓↓ever␈↓␈αmatter?␈α"+"␈αand␈α"*"␈αare␈αexamples␈αof␈αarithmetic␈αfunctions;␈αcan␈αwe␈αalways␈αleave
␈↓ ↓H␈↓the␈α∞order␈α∂of␈α∞evaluation␈α∂unspecified␈α∞for␈α∞arithmetic␈α∂functions?␈α∞How␈α∂about␈α∞evaluation␈α∂of␈α∞arbitrary
␈↓ ↓H␈↓␈↓↓88  Evaluation␈↓ 44.1␈↓


␈↓ ↓H␈↓functional␈αexpressions?␈α If␈αthe␈αorder␈αdoesn't␈αmatter,␈αthen␈αthe␈αspecification␈αof␈αthe␈αevaluation␈αprocess
␈↓ ↓H␈↓becomes much simpler. If it ␈↓↓does␈↓ matter then we must know why and where.

␈↓ ↓H␈↓We␈α
have␈α
seen␈α
that␈α
the␈α
order␈α
of␈α
evaluation␈α
␈↓↓can␈↓␈α
make␈α
a␈α
difference␈α
in␈α
LISP.␈α
 On␈α
page 24␈α∞we␈α
saw
␈↓ ↓H␈↓that␈αorder␈αof␈αevaluation␈αin␈αconditional␈αexpressions␈αcan␈αmake␈αa␈αdifference.␈αOn␈αpage 19␈αwe␈αsaw␈α
that
␈↓ ↓H␈↓␈↓	CBV␈↓,␈αLISP's␈α
computational␈αinterpretation␈α
of␈αfunction␈αevaluation,␈α
requires␈αsome␈α
care.␈α Since␈αwe␈α
are
␈↓ ↓H␈↓using␈α␈↓	CBV␈↓␈αwe␈αmust␈αmake␈α␈↓↓some␈↓␈αdecision␈αregarding␈αthe␈αorder␈αof␈αevaluation␈αof␈αthe␈αarguments␈αto␈αa
␈↓ ↓H␈↓function␈α∞call,␈α∂say␈α∞␈↓αf[t␈↓β1␈↓α;t␈↓β2␈↓α;␈α∞...t␈↓βn␈↓].␈α∂ We␈α∞will␈α∂assume␈α∞that␈α∞we␈α∂will␈α∞evaluate␈α∞the␈α∂arguments␈α∞from␈α∂left␈α∞to
␈↓ ↓H␈↓right.

␈↓ ↓H␈↓Consider the example due to J. Morris:

␈↓ ↓H␈↓␈↓ ∧N␈↓αf[x;y] <= [x = 0 → 0; ␈↓
t␈↓α → f[x-1;f[y-2;x]]]␈↓.

␈↓ ↓H␈↓Evaluation of ␈↓αf[2;1]␈↓ will terminate if we always evaluate the outermost occurrence of ␈↓αf␈↓.  Thus:

␈↓ ↓H␈↓␈↓ ∧9␈↓αf[2;1] = f[1;f[-1;2]] = f[0;f[f[-1;2]-2;1]] = 0;␈↓ 

␈↓ ↓H␈↓However if we evaluate the innermost occurrences␈↓π 46␈↓ first, the computation will not terminate:

␈↓ ↓H␈↓␈↓ βf␈↓αf[2;1] = f[1;f[-1;2]] = f[1;f[-2;f[0;-1]]] = f[1;f[-2;0]] = ... .␈↓ 

␈↓ ↓H␈↓So␈αthe␈α
choice␈αof␈α
evaluation␈αschemes␈α
is,␈αindeed,␈αfraught␈α
with␈αperil.␈α
 The␈αevaluation␈α
scheme,␈α␈↓	CBV␈↓,
␈↓ ↓H␈↓which␈α∞we␈α∞chose␈α
is␈α∞called␈α∞␈↓↓call-by-value␈↓␈α
and␈α∞is␈α∞an␈α
␈↓↓inside-out␈↓␈α∞style␈α∞of␈α
evaluation,␈α∞meaning␈α∞that␈α
we
␈↓ ↓H␈↓operate␈αon␈α
the␈αsubexpressions␈α
before␈αtackling␈α
the␈αmain␈α
expression.␈α Alternative␈α
proposals␈αexist␈α
and
␈↓ ↓H␈↓have␈α∪their␈α∪merits;␈α∪call-by-name␈α∪evaluation␈α∩is␈α∪another␈α∪well-known␈α∪scheme.␈α∪We␈α∪advertised␈α∩this
␈↓ ↓H␈↓outside-in␈α∩scheme␈α∪on␈α∩page 20␈α∩as␈α∪␈↓	CBN␈↓.␈α∩ From␈α∪a␈α∩computational␈α∩perspective,␈α∪we␈α∩can␈α∪live␈α∩with
␈↓ ↓H␈↓call-by-value,␈α∞though␈α∞we␈α∞know␈α∞the␈α∞use␈α∞of␈α∞such␈α∞a␈α∞rule␈α∞may␈α∞lead␈α∞to␈α∞non-terminating␈α∞computations
␈↓ ↓H␈↓when call-by-name would run to completion.

␈↓ ↓H␈↓Intuitively,␈αcall-by-value␈αsays:␈αevaluate␈αthe␈αarguments␈αto␈αa␈αfunction␈αbefore␈αyou␈αattempt␈αto␈αapply␈αthe
␈↓ ↓H␈↓function␈α⊂definition␈α⊂to␈α⊂the␈α⊂arguments.␈α⊂ Let's␈α⊂look␈α∂at␈α⊂a␈α⊂simple␈α⊂arithmetic␈α⊂example.␈α⊂ Let␈α⊂␈↓αf[x;y]␈↓␈α∂be
␈↓ ↓H␈↓␈↓αx␈↓π2␈↓α + y␈↓␈α
and␈α∞consider␈α
␈↓αf[3+4;2*2]␈↓.␈α∞ Then␈α
call-by-value␈α∞says␈α
evaluate␈α∞the␈α
arguments,␈α∞getting␈α
␈↓α7␈↓␈α∞and␈α
␈↓α4␈↓;
␈↓ ↓H␈↓associate␈α
those␈α
values␈α
with␈α
the␈α
formal␈αparameters␈α
of␈α
␈↓αf␈↓␈α
(i.e.␈α
␈↓α7␈↓␈α
with␈α␈↓αx␈↓␈α
and␈α
␈↓α4␈↓␈α
with␈α
␈↓αy␈↓)␈α
and␈αthen␈α
evaluate
␈↓ ↓H␈↓the body of ␈↓αf␈↓ resulting in  ␈↓α7␈↓π2␈↓α + 4 = 53␈↓. This is the scheme we captured in Section 3.6.

␈↓ ↓H␈↓Call-by-name␈α⊃says␈α⊂pass␈α⊃the␈α⊃␈↓↓unevaluated␈↓␈α⊂actual␈α⊃parameters␈α⊃to␈α⊂the␈α⊃function,␈α⊃giving␈α⊂␈↓α(3+4)␈↓π2␈↓α + 2*2␈↓
␈↓ ↓H␈↓which␈α∞also␈α∞evaluates␈α∞to␈α∞␈↓α53␈↓.␈α∞Call-by-name␈α
is␈α∞essentially␈α∞a␈α∞"substitution␈α∞followed␈α∞by␈α
simplification"
␈↓ ↓H␈↓system␈α∂of␈α⊂computation.␈α∂ We␈α⊂will␈α∂say␈α⊂more␈α∂about␈α∂call-by-name␈α⊂and␈α∂other␈α⊂styles␈α∂of␈α⊂evaluation␈α∂in
␈↓ ↓H␈↓Section 4.15.  Most of this section will be restricted to call-by-value.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 46␈↓␈α⊂The␈α⊃notions␈α⊂of␈α⊃"innermost"␈α⊂and␈α⊃"outermost"␈α⊂evaluation␈α⊃need␈α⊂to␈α⊃be␈α⊂slightly␈α⊃embellished␈α⊂for
␈↓ ↓H␈↓general␈αfunction␈αapplication.␈αIf␈αthe␈αchosen␈αapplication␈αhas␈αseveral␈αarguments,␈αthen␈αwe␈αmust␈αspecify
␈↓ ↓H␈↓an␈α
order␈α
for␈α
their␈α
evaluation.␈αThus␈α
terms␈α
like␈α
"outermost-leftmost"␈α
and␈α"innermost-rightmost"␈α
occur.
␈↓ ↓H␈↓For example, the LISP scheme is an instance of "innermost-leftmost" evaluation.
␈↓ ↓H␈↓␈↓↓4.1␈↓ 	↑Introduction     89␈↓


␈↓ ↓H␈↓If␈α∂you␈α∂look␈α∂at␈α∂the␈α∂structure␈α∂of␈α∂␈↓αvalue␈↓λ''␈↓␈α∂and␈α∂␈↓αapply␈↓λ'␈↓␈α∂beginning␈α∂on␈α∂page 78␈α∂you␈α∂will␈α∂see␈α∂that␈α∂they
␈↓ ↓H␈↓encode␈α∪the␈α∪␈↓	CBV␈↓␈α∪philosophy,␈α∪are␈α∪recursive,␈α∩and␈α∪have␈α∪an␈α∪intended␈α∪interpretation␈α∪which␈α∩goes
␈↓ ↓H␈↓something like this:

␈↓ ↓H␈↓␈↓↓1.␈↓␈αIf␈αthe␈α
expression␈αis␈αa␈α
constant␈αthen␈αthe␈α
value␈αof␈αthe␈α
expression␈αis␈αthat␈α
constant.␈α (The␈αvalue␈αof␈α
␈↓α3␈↓
␈↓ ↓H␈↓is ␈↓α3␈↓ ␈↓π 47␈↓).

␈↓ ↓H␈↓␈↓↓2.␈↓␈αIf␈α
the␈αexpression␈α
is␈αa␈αvariable␈α
then␈αsee␈α
what␈αthe␈αcurrent␈α
value␈αassociated␈α
with␈αthat␈α
variable␈αis.
␈↓ ↓H␈↓Within the evaluation of, say, ␈↓αf[3;4]␈↓ where ␈↓αf[x;y] <= x␈↓π2␈↓α + y ␈↓the current value of the variable ␈↓αx␈↓ is ␈↓α3␈↓.

␈↓ ↓H␈↓␈↓↓3.␈↓␈αThe␈αonly␈αother␈α
kind␈αof␈αarithmetic␈αexpression␈α
that␈αwe␈αcan␈αhave␈α
is␈αa␈αfunction␈αname␈α
followed␈αby
␈↓ ↓H␈↓arguments,␈αfor␈αexample␈α␈↓αf[3;4]␈↓.␈α In␈αthis␈αcase␈αwe␈αfirst␈αevaluate␈αthe␈αarguments␈α␈↓π 48␈↓␈αand␈αthen␈αapply␈αthe
␈↓ ↓H␈↓definition␈αof␈αthe␈αfunction␈αto␈αthose␈αevaluated␈αarguments.␈α How␈αdo␈αwe␈αapply␈αthe␈αfunction␈αdefinition
␈↓ ↓H␈↓to␈α⊂the␈α∂evaluated␈α⊂arguments?␈α⊂ We␈α∂associate␈α⊂or␈α⊂bind␈α∂the␈α⊂formal␈α⊂parameters␈α∂(or␈α⊂variables)␈α⊂of␈α∂the
␈↓ ↓H␈↓definition␈α∞to␈α
the␈α∞values␈α
of␈α∞the␈α
actual␈α∞parameters.␈α
 We␈α∞then␈α
evaluate␈α∞the␈α
body␈α∞of␈α
the␈α∞function␈α
in
␈↓ ↓H␈↓this␈α⊂new␈α⊂environment.␈α⊂ Notice␈α⊂that␈α⊂we␈α⊂do␈α∂␈↓↓not␈↓␈α⊂explicitly␈α⊂substitute␈α⊂the␈α⊂values␈α⊂for␈α⊂the␈α∂variables
␈↓ ↓H␈↓which appear in an expression. We simulate substitutions by table lookup.

␈↓ ↓H␈↓A␈α
moment's␈α
reflection␈αon␈α
the␈α
informal␈α
evaluation␈αprocess␈α
we␈α
use␈α
in␈αLISP␈α
should␈α
convince␈α
us␈αof␈α
the
␈↓ ↓H␈↓plausibility␈α∩of␈α∩describing␈α∪LISP␈α∩evaluation␈α∩at␈α∪a␈α∩similar␈α∩level␈α∩of␈α∪precision.␈α∩ First,␈α∩if␈α∪the␈α∩LISP
␈↓ ↓H␈↓expression␈αis␈αa␈αconstant,␈αthen␈αthe␈αvalue␈αof␈αthe␈αexpression␈αis␈αthat␈αconstant.␈α What␈αare␈αthe␈αconstants
␈↓ ↓H␈↓of␈α
LISP?␈α
They're␈α
just␈α
the␈α
S-exprs.␈αThus␈α
the␈α
value␈α
of␈α
␈↓α(A . B)␈↓␈α
is␈α
␈↓α(A . B)␈↓,␈αjust␈α
like␈α
the␈α
value␈α
of␈α
␈↓α3␈↓␈αis␈α
␈↓α3␈↓.
␈↓ ↓H␈↓Variables␈α∞and␈α
functional␈α∞applications␈α
appear␈α∞in␈α∞LISP␈α
and␈α∞are␈α
handled␈α∞as␈α
described␈α∞above␈α∞in␈α
␈↓↓2.␈↓
␈↓ ↓H␈↓and␈α␈↓↓3.␈↓.␈α The␈αadditional␈αartifact␈αof␈αLISP␈αwhich␈αwe␈αhave␈αto␈αinclude␈αin␈αa␈αdiscussion␈αof␈αevaluation␈αis
␈↓ ↓H␈↓the␈αconditional␈αexpression.␈αBut␈α
clearly␈αits␈αevaluation␈αcan␈αalso␈α
be␈αprecisely␈αspecified.␈αWe␈αdid␈α
so␈αon
␈↓ ↓H␈↓page 23.␈α∀ So,␈α∀in␈α∪more␈α∀specific␈α∀detail,␈α∪here␈α∀is␈α∀some␈α∪of␈α∀the␈α∀structure␈α∪of␈α∀the␈α∀LISP␈α∪evaluation
␈↓ ↓H␈↓mechanism:

␈↓ ↓H␈↓␈↓↓1.␈↓ If the expression to be evaluated is a constant then the value is that constant.

␈↓ ↓H␈↓␈↓↓2.␈↓ If the expression is a variable find its value in the current environment.

␈↓ ↓H␈↓␈↓↓3.␈↓␈αIf␈αthe␈αexpression␈αis␈αa␈αconditional␈αexpression␈αthen␈αit␈αis␈αof␈αthe␈αform␈α␈↓α[p␈↓β1␈↓α␈α→␈αe␈↓β1␈↓α;␈αp␈↓β2␈↓α␈α→␈αe␈↓β2␈↓α;␈α...␈αp␈↓βn␈↓α␈α→␈αe␈↓βn␈↓].
␈↓ ↓H␈↓Evaluate it using the semantics defined on page 23.

␈↓ ↓H␈↓␈↓↓4.␈↓ If the expression is of the form: ␈↓αf␈↓[␈↓αt␈↓β1␈↓α;t␈↓β2␈↓α; ... ;t␈↓βn␈↓α]␈↓ then:






␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 47␈↓ We are ignoring the distinction between the ␈↓↓numeral␈↓ ␈↓α3␈↓ and the ␈↓↓number␈↓α 3.␈↓

␈↓ ↓H␈↓␈↓π 48␈↓ Here we are using the evaluation process recursively.
␈↓ ↓H␈↓␈↓↓90  Evaluation␈↓ 44.1␈↓



␈↓ ↓H␈↓␈↓ αh␈↓↓a.␈↓ Evaluate the arguments ␈↓αt␈↓β1␈↓α, t␈↓β2␈↓α, ... ,t␈↓βn␈↓ from left to right.

␈↓ ↓H␈↓␈↓ αh␈↓↓b.␈↓ Find the definition of the function, ␈↓αf␈↓.

␈↓ ↓H␈↓␈↓ αh␈↓↓c.␈↓␈α∂Associate␈α∂the␈α∂evaluated␈α∂arguments␈α∂with␈α∂the␈α∂formal␈α∂parameters␈α∂in␈α∂the
␈↓ ↓H␈↓␈↓ αhfunction definition.

␈↓ ↓H␈↓␈↓ αh␈↓↓d.␈↓␈αEvaluate␈αthe␈αbody␈αof␈αthe␈αfunction,␈αwhile␈αremembering␈αthe␈αvalues␈αof␈αthe
␈↓ ↓H␈↓␈↓ αhvariables.

␈↓ ↓H␈↓We␈α∞have␈α∞seen␈α∞(Section 3.6)␈α∞that␈α∞we␈α∞can␈α∞transcribe␈α∞a␈α∞simple␈α∞kind␈α∞of␈α∞arithmetic␈α∞evaluation␈α∂into␈α∞a
␈↓ ↓H␈↓recursive␈α∪LISP␈α∪program.␈α∪ That␈α∪program␈α∪operates␈α∪on␈α∪a␈α∪representation␈α∪of␈α∪the␈α∪expression␈α∩and
␈↓ ↓H␈↓produces␈αthe␈αvalue.␈αMost␈αof␈αour␈αwork␈αin␈αthat␈αexample␈αwas␈αdone␈αwithout␈αgiving␈αexplicit␈α
details␈αof
␈↓ ↓H␈↓the␈α⊂representation.␈α⊂ However␈α⊃when␈α⊂we␈α⊂discussed␈α⊂the␈α⊃representation␈α⊂of␈α⊂simple␈α⊃differentiation␈α⊂in
␈↓ ↓H␈↓Section 3.3 we showed a detailed representation.

␈↓ ↓H␈↓We␈α∩have␈α∩demonstrated␈α∩an␈α⊃informal,␈α∩but␈α∩reasonably␈α∩precise,␈α⊃evaluation␈α∩scheme␈α∩for␈α∩LISP;␈α⊃our
␈↓ ↓H␈↓discussion␈αis␈αready␈αfor␈αa␈αmore␈αformal␈αdevelopment.␈α It␈αshould␈αbe␈αclear␈αthat␈αwe␈αcould␈αwrite␈αa␈αLISP
␈↓ ↓H␈↓function␈αrepresenting␈α
the␈αevaluation␈α
process␈αprovided␈α
that␈αwe␈α
can␈αfind␈α
a␈αrepresentation␈α
for␈αLISP
␈↓ ↓H␈↓expressions␈αas␈αS-expressions.␈αThis␈αmapping,␈α␈↓
R␈↓,␈αof␈α
LISP␈αexpressions␈αto␈αS-exprs␈αis␈αour␈αfirst␈αorder␈α
of
␈↓ ↓H␈↓business.␈α⊂We␈α∂will␈α⊂accomplish␈α∂this␈α⊂mapping␈α⊂by␈α∂using␈α⊂an␈α∂extension␈α⊂of␈α∂the␈α⊂scheme␈α⊂introduced␈α∂in
␈↓ ↓H␈↓Section 3.3.␈α∩ We␈α∩plan␈α∩to␈α∩expend␈α∩some␈α∩effort␈α∩in␈α∩describing␈α∩a␈α∩specific␈α∩representation␈α∩for␈α∩LISP
␈↓ ↓H␈↓expressions␈α∪for␈α∪two␈α∪reasons.␈α∪First,␈α∪though␈α∀abstraction␈α∪is␈α∪a␈α∪most␈α∪desirable␈α∪attribute,␈α∀we␈α∪must
␈↓ ↓H␈↓reconcile␈α∪our␈α∪abstraction␈α∩with␈α∪reality;␈α∪our␈α∪programs␈α∩must␈α∪run.␈α∪The␈α∩second␈α∪point␈α∪is␈α∪that␈α∩the
␈↓ ↓H␈↓representation␈α
we␈α
pick␈α
will␈α
have␈α
a␈α
very␈α
close␈α
relationship␈α
to␈α
the␈α
way␈α
we␈α
present␈α
programs␈α∞to␈α
the
␈↓ ↓H␈↓machine.␈α⊃So␈α∩we␈α⊃will␈α∩be␈α⊃careful␈α⊃and␈α∩thorough␈α⊃in␈α∩describing␈α⊃the␈α⊃mapping␈α∩and␈α⊃you␈α∩should␈α⊃be
␈↓ ↓H␈↓conscientious␈α∞in␈α∞your␈α∞understanding␈α∞of␈α∞that␈α
mapping.␈α∞ Once␈α∞that␈α∞representation␈α∞is␈α∞given␈α∞we␈α
will
␈↓ ↓H␈↓produce a LISP algorithm which describes the evaluation process used in LISP.

␈↓ ↓H␈↓This␈αprocess␈αof␈αmapping␈αLISP␈αexpressions␈αonto␈αS-exprs␈αand␈αwriting␈αa␈αLISP␈αfunction␈αto␈αact␈αas␈αan
␈↓ ↓H␈↓evaluator␈α⊃may␈α⊃seem␈α⊃a␈α⊃bit␈α∩incestuous;␈α⊃indeed,␈α⊃the␈α⊃rationale␈α⊃for␈α∩doing␈α⊃any␈α⊃of␈α⊃this␈α⊃may␈α∩not␈α⊃be
␈↓ ↓H␈↓apparent.␈α∀ Patience,␈α∀please.␈α∀ The␈α∀mapping␈α∀is␈α∀no␈α∀more␈α∀obscure␈α∀than␈α∀that␈α∀in␈α∀the␈α∪polynomial
␈↓ ↓H␈↓evaluation␈αor␈αdifferentiation␈αexamples.␈α It␈αis␈αjust␈αanother␈αinstance␈αof␈αthe␈αdiagram␈αof␈αpage 55,␈αonly
␈↓ ↓H␈↓now␈α
we␈α
are␈α
applying␈α
the␈α
process␈α
to␈α
LISP␈α
itself.␈α The␈α
effect␈α
is␈α
to␈α
force␈α
us␈α
to␈α
make␈α
precise␈αexactly
␈↓ ↓H␈↓what is meant by LISP evaluation. This precision will have many important ramifications.

␈↓ ↓H␈↓Also,␈α⊂we've␈α∂been␈α⊂doing␈α∂evaluation␈α⊂of␈α∂S-expr␈α⊂representations␈α∂of␈α⊂LISP␈α∂expressions␈α⊂already.␈α∂ The
␈↓ ↓H␈↓␈↓↓great␈α∪mother␈α∪of␈α∀all␈α∪functions␈↓␈α∪is␈α∀exactly␈α∪the␈α∪evaluation␈α∀mechanism␈α∪for␈α∪the␈α∀LISP␈α∪primitive
␈↓ ↓H␈↓functions␈αand␈α
predicates,␈α␈↓αcar,␈αcdr,␈α
cons,␈αatom␈↓␈αand␈α
␈↓αeq␈↓␈αwhen␈αrestricted␈α
to␈αfunctional␈α
composition␈αand
␈↓ ↓H␈↓constant arguments.  The ␈↓↓great mother revisited␈↓ is the extension to conditional expressions.

␈↓ ↓H␈↓In␈α
the␈α
next␈α∞section␈α
we␈α
will␈α
give␈α∞a␈α
typical␈α
mapping␈α
of␈α∞LISP␈α
expressions␈α
to␈α
elements␈α∞of␈α
␈↓<sexpr>␈↓.
␈↓ ↓H␈↓But␈α⊂remember␈α⊂that␈α⊂we␈α⊂should␈α⊂attempt␈α⊂to␈α⊂keep␈α⊂the␈α⊂knowledge␈α⊂of␈α⊂the␈α⊂representation␈α⊂out␈α⊂of␈α⊂the
␈↓ ↓H␈↓structure␈αof␈αthe␈αalgorithm.␈α Let's␈αstop␈αfor␈αsome␈αexamples␈αof␈αtranslating␈αLISP␈αfunctions␈αinto␈αS-expr
␈↓ ↓H␈↓notation.
␈↓ ↓H␈↓␈↓↓4.2␈↓ εzS-expr translation of LISP expressions     91␈↓


␈↓ ↓H␈↓␈↓ ∧,␈↓↓4.2  S-expr translation of LISP expressions␈↓


␈↓ ↓H␈↓We␈α
will␈α
go␈αthrough␈α
the␈α
list␈αof␈α
LISP␈α
constructs,␈αdescribing␈α
the␈α
effect␈αof␈α
the␈α
representational␈αmap,␈α
␈↓
R␈↓,
␈↓ ↓H␈↓and give a few examples applying ␈↓
R␈↓.

␈↓ ↓H␈↓We will represent numerals just as numerals, e.g.:
␈↓ ↓H␈↓␈↓ ¬
␈↓
R␈↓∞( ␈↓<numeral> ␈↓∞)␈↓α = ␈↓<numeral>

␈↓ ↓H␈↓␈↓ εβ␈↓
R␈↓∞( ␈↓α2 ␈↓∞)␈↓α = 2

␈↓ ↓H␈↓We will translate identifiers to their upper-case counterpart. Thus:
␈↓ ↓H␈↓␈↓ ∧o␈↓
R␈↓∞( ␈↓<identifier> ␈↓∞)␈↓α = ␈↓<literal atom>
␈↓ ↓H␈↓␈↓ ε↓␈↓
R␈↓∞( ␈↓αx ␈↓∞)␈↓α = X
␈↓ ↓H␈↓α␈↓ ¬s␈↓
R␈↓∞( ␈↓αy2 ␈↓∞)␈↓α = Y2
␈↓ ↓H␈↓α␈↓ ¬b␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α = CAR

␈↓ ↓H␈↓Now␈αwe've␈αgot␈αa␈αproblem:␈αWe␈αwish␈αto␈αmap␈αarbitrary␈αLISP␈αexpressions␈αto␈αS-expressions.␈αThe␈α
LISP
␈↓ ↓H␈↓expression␈α∩␈↓αx␈↓␈α⊃translates␈α∩to␈α⊃␈↓αX␈↓.␈α∩␈↓αX␈↓␈α⊃is␈α∩itself␈α⊃a␈α∩LISP␈α⊃expression␈α∩(a␈α⊃constant);␈α∩␈↓↓it␈↓␈α⊃must␈α∩also␈α∩have␈α⊃a
␈↓ ↓H␈↓translation.␈α We␈αmust␈αbe␈αa␈αlittle␈αcareful␈αhere.␈αWhen␈αwe␈αwrite␈αSon␈αof␈αGreat␈αMother␈αwe␈αwill␈αgive␈αit
␈↓ ↓H␈↓an␈α
S-expr␈α
representation␈α
of␈α
a␈α
form␈α
to␈α
be␈αevaluated.␈α
We␈α
might␈α
give␈α
it␈α
the␈α
representation␈α
of␈α␈↓αcar[x]␈↓␈α
in
␈↓ ↓H␈↓which␈αcase␈αthe␈αvalue␈αcomputed␈αwill␈αdepend␈αon␈αthe␈αcurrent␈αvalue␈αbound␈αto␈α␈↓αx␈↓.␈α We␈αmight␈αalso␈αgive
␈↓ ↓H␈↓the␈α∂representation␈α∂of␈α∂␈↓αcar[X]␈↓;␈α∂in␈α∂this␈α∂case␈α∂we␈α∂should␈α∂expect␈α∂to␈α∂be␈α∂presented␈α∂with␈α∂␈↓λB␈↓␈α∂or␈α⊂an␈α∂error
␈↓ ↓H␈↓message.␈α
 Or,␈αfor␈α
example,␈αsome␈α
function␈α
␈↓αfoo␈↓␈αwe␈α
wish␈αto␈α
write␈α
may␈αreturn␈α
the␈αS-expr␈α
representation
␈↓ ↓H␈↓of␈αa␈αLISP␈αform␈αas␈αits␈αvalue.␈α Say␈α␈↓αfoo[1]␈↓␈αreturns␈αthe␈αrepresentation␈αof␈α␈↓αcar[x]␈↓␈αand␈α␈↓αfoo[2]␈↓␈α
returns␈αthe
␈↓ ↓H␈↓representation␈α∞of␈α∞␈↓αcar[X]␈↓.␈α∞We␈α∞must␈α∞be␈α
able␈α∞to␈α∞distinguish␈α∞between␈α∞these␈α∞representations.␈α∞ That␈α
is,
␈↓ ↓H␈↓given␈αthe␈α
representation,␈αthere␈α
should␈αbe␈α
exactly␈αone␈α
way␈αof␈α
interpreting␈αit␈α
as␈αa␈α
LISP␈αexpression.
␈↓ ↓H␈↓The␈α
mapping␈α
must␈α
be␈α
1-1.␈α∞So␈α
we␈α
must␈α
represent␈α
␈↓αx␈↓␈α∞and␈α
␈↓αX␈↓␈α
as␈α
␈↓↓different␈↓␈α
S-exprs.␈α∞ The␈α
translation
␈↓ ↓H␈↓scheme we pick is: for any S-expression ␈↓λα␈↓, its translation is ␈↓α(QUOTE ␈↓λα␈↓α)␈↓.
␈↓ ↓H␈↓␈↓ ∧j␈↓
R␈↓∞( ␈↓<sexpr> ␈↓∞)␈↓α = (QUOTE ␈↓<sexpr>␈↓α)␈↓

␈↓ ↓H␈↓For example:
␈↓ ↓H␈↓␈↓ ¬9␈↓
R␈↓∞( ␈↓αX ␈↓∞)␈↓α = (QUOTE X)␈↓
␈↓ ↓H␈↓␈↓ ∧⎇␈↓
R␈↓∞( ␈↓α(A .B) ␈↓∞)␈↓α = (QUOTE (A . B))␈↓
␈↓ ↓H␈↓␈↓ ∧i␈↓
R␈↓∞( ␈↓αQUOTE ␈↓∞)␈↓α = (QUOTE QUOTE)␈↓

␈↓ ↓H␈↓We␈α⊃must␈α⊂also␈α⊃show␈α⊂how␈α⊃to␈α⊂map␈α⊃expressions␈α⊂of␈α⊃the␈α⊂form␈α⊃␈↓αf[e␈↓β1␈↓α ; ... ;e␈↓βn␈↓α]␈↓␈α⊂onto␈α⊃S-exprs.␈α⊃ We␈α⊂have
␈↓ ↓H␈↓already␈αseen␈αone␈α
satisfactory␈αmapping␈αfor␈αfunctions␈α
in␈αprefix␈αform␈αin␈α
Section 3.3.␈α We␈αwill␈αuse␈α
that
␈↓ ↓H␈↓mapping, called Cambridge Polish␈↓π 49␈↓, here. That is:

␈↓ ↓H␈↓␈↓ β3␈↓
R␈↓∞( ␈↓αf[e␈↓β1␈↓α;e␈↓β2␈↓α; ...;e␈↓βn␈↓α] ␈↓∞)␈↓α = ( ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓β1␈↓α ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓β2␈↓α ␈↓∞)␈↓α ... ␈↓
R␈↓∞(␈↓α e␈↓βn␈↓α ␈↓∞)␈↓α )␈↓ 

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 49␈↓␈α⊃The␈α⊃name,␈α⊃Cambridge␈α∩Polish,␈α⊃is␈α⊃derived␈α⊃from␈α⊃two␈α∩sources:␈α⊃Cambridge,␈α⊃since␈α⊃M.I.T.␈α∩is␈α⊃in
␈↓ ↓H␈↓Cambridge␈α∞Massachusetts,␈α∞and␈α∞McCarthy␈α
was␈α∞at␈α∞M.I.T.␈α∞while␈α
developing␈α∞his␈α∞ideas;␈α∞Polish,␈α
since
␈↓ ↓H␈↓the representation is a dialect of a notation developed by a school of Polish logicians.
␈↓ ↓H␈↓␈↓↓92  Evaluation␈↓ 24.2␈↓


␈↓ ↓H␈↓Examples:␈↓α
␈↓ ↓H␈↓α␈↓ ∧ ␈↓
R␈↓∞( ␈↓αcar[x] ␈↓∞)␈↓α = (␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α ␈↓
R␈↓∞( ␈↓αx ␈↓∞)␈↓α ) = (CAR X)
␈↓ ↓H␈↓α␈↓ βU␈↓
R␈↓∞( ␈↓αcar[X] ␈↓∞)␈↓α = (␈↓
R␈↓∞( ␈↓αcar ␈↓∞)␈↓α ␈↓
R␈↓∞( ␈↓αX ␈↓∞)␈↓α ) = (CAR (QUOTE X))
␈↓ ↓H␈↓α␈↓ β0␈↓
R␈↓∞( ␈↓αcons[cdr[(A .B)];x]  ␈↓∞)␈↓α = (CONS (CDR (QUOTE (A . B))) X)

␈↓ ↓H␈↓α␈↓The␈α␈↓
R␈↓-mapping␈αmust␈α
handle␈αconditional␈αexpressions.␈α
A␈αconditional␈αis␈α
represented␈αas␈αa␈α
list␈αwhose
␈↓ ↓H␈↓first␈αelement␈α
is␈α␈↓αCOND␈↓␈α
and␈αwhose␈α
next␈α␈↓αn␈↓␈α
elements␈αare␈α
representations␈αof␈α
the␈α␈↓αp␈↓βi␈↓α-e␈↓βi␈↓␈α
pairs.␈αThe␈α
␈↓
R␈↓-map
␈↓ ↓H␈↓of such pairs is a list of the ␈↓
R␈↓-maps of the two elements:

␈↓ ↓H␈↓␈↓ α3␈↓
R␈↓∞( ␈↓α[p␈↓β1␈↓α → e␈↓β1␈↓α; ... ;p␈↓βn␈↓α → e␈↓βn␈↓α] ␈↓∞)␈↓α = (COND (␈↓
R␈↓∞(␈↓α p␈↓β1 ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓β1␈↓∞)␈↓α ) ... (␈↓
R␈↓∞(␈↓α p␈↓βn␈↓α ␈↓∞)␈↓α ␈↓
R␈↓∞(␈↓α e␈↓βn ␈↓∞)␈↓α)) 

␈↓ ↓H␈↓α␈↓An example:␈↓α

␈↓ ↓H␈↓α␈↓ αd␈↓
R␈↓∞( ␈↓α[atom[x] →1; q[y] → X] ␈↓∞)␈↓α = (COND ((ATOM X) 1) ((Q Y) (QUOTE X))) 

␈↓ ↓H␈↓Notice␈αthat␈α
␈↓α(COND ... )␈↓␈αand␈α
␈↓α(QUOTE ... )␈↓␈α␈↓↓look␈↓␈α
like␈αtranslations␈α
of␈αfunction␈α
applications␈αof␈αthe␈α
form
␈↓ ↓H␈↓␈↓αcond[ ... ]␈α␈↓␈αand␈α␈↓αquote[ ... ]␈↓.␈αHowever␈αsince␈αwe␈αexpect␈αapplication␈αto␈αbe␈αperformed␈αusing␈αcall-by-value,
␈↓ ↓H␈↓we␈αmust␈αhandle␈αthese␈αconstructs␈αin␈αa␈αspecial␈αmanner.␈α Indeed,␈αnote␈αthat␈α␈↓αquote[␈↓λα␈↓α]␈↓␈αstands␈αfor␈α␈↓
R␈↓∞(␈↓λα␈↓∞)␈↓,
␈↓ ↓H␈↓and␈α
that␈α
the␈α
"arguments"␈αto␈α
␈↓αcond␈↓␈α
are␈α
not␈αto␈α
be␈α
interpreted␈α
as␈αsome␈α
kind␈α
of␈α
function␈αapplications.
␈↓ ↓H␈↓For example, ␈↓αCOND ((ATOM X) 1) ...)␈↓ doesn't represent ␈↓αcond[ atom[x][1]; ... ]␈↓.

␈↓ ↓H␈↓Finally, the translations of the truth values ␈↓
t␈↓ and ␈↓
f␈↓ will be ␈↓αT␈↓ and ␈↓αNIL␈↓, respectively.
␈↓ ↓H␈↓␈↓ ε	␈↓
R␈↓∞( ␈↓
t␈↓∞ )␈↓α = T
␈↓ ↓H␈↓α␈↓ ¬v␈↓
R␈↓∞( ␈↓
f␈↓∞ )␈↓α = NIL

␈↓ ↓H␈↓You␈α
might␈α∞have␈α
noticed␈α∞that␈α
these␈α∞last␈α
two␈α∞applications␈α
of␈α∞the␈α
␈↓
R␈↓-mapping␈α∞have␈α
the␈α∞potential␈α
to
␈↓ ↓H␈↓cause trouble. They will spoil the 1-1 property of ␈↓
R␈↓:
␈↓ ↓H␈↓␈↓ ε
␈↓
R␈↓∞( ␈↓αt␈↓∞ )␈↓α = T
␈↓ ↓H␈↓α␈↓ ¬j␈↓
R␈↓∞( ␈↓αnil␈↓∞ )␈↓α = NIL

␈↓ ↓H␈↓The usual way to escape from this difficulty is to outlaw ␈↓αt␈↓ and ␈↓αnil␈↓ as LISP variables␈↓π 50␈↓.

␈↓ ↓H␈↓Perhaps␈α∀our␈α∀concern␈α∀for␈α∀the␈α∀␈↓
R␈↓-mapping's␈α∀properties␈α∀appears␈α∀heavy-handed␈α∀where␈α∀a␈α∀simple
␈↓ ↓H␈↓solution␈α
seems␈αapparent:␈α
␈↓
t␈↓␈αis␈α
␈↓
t␈↓␈α
and␈α␈↓αt␈↓␈α
is␈α␈↓αt␈↓;␈α
when␈αwe␈α
want␈α
the␈αtruth␈α
value␈αwe␈α
write␈α␈↓
t␈↓␈α
and␈α
when␈αwe
␈↓ ↓H␈↓want␈α
the␈α
variable␈α
we␈α
write␈α
␈↓αt␈↓.␈α
The␈α
problem␈α
is␈α
that␈α
when␈α
we␈α
write␈α
programs␈α
in␈α
a␈α
format␈α∞which␈α
a
␈↓ ↓H␈↓machine␈α
version␈α
of␈α∞LISP␈α
will␈α
understand,␈α
we␈α∞will␈α
be␈α
writing␈α
the␈α∞␈↓
R␈↓-image,␈α
rather␈α
than␈α∞the␈α
LISP
␈↓ ↓H␈↓expression␈α∃form.␈α∀Thus␈α∃to␈α∃ask␈α∀a␈α∃LISP␈α∃machine␈α∀to␈α∃evaluate␈α∃␈↓αcar[(A . B)]␈↓␈α∀we␈α∃present␈α∃it␈α∀with
␈↓ ↓H␈↓␈↓α(CAR (QUOTE (A . B)))␈↓.␈α What␈αthis␈αmeans␈αis␈αthat␈αwe␈αare␈αpresenting␈αour␈αprograms␈αto␈αthe␈αmachine
␈↓ ↓H␈↓as␈α∞data␈α
structures␈α∞of␈α
the␈α∞language.␈α
 It␈α∞would␈α∞be␈α
like␈α∞expressing␈α
programs␈α∞in␈α
Fortran␈α∞or␈α∞Algol␈α
as
␈↓ ↓H␈↓arrays␈αof␈αintegers;␈αthat␈αis,␈αthe␈αdata␈αstructures␈αof␈α␈↓↓those␈↓␈αlanguages.␈α We␈αwill␈αexplore␈αthe␈αimplications
␈↓ ↓H␈↓of␈αthis␈αapproach␈αto␈αprogramming␈αin␈αlater␈αsections,␈αbut␈αfor␈αnow␈αit␈αshould␈αhelp␈αto␈αknow␈αthat␈αwe␈αwill
␈↓ ↓H␈↓be making extensive use of the ␈↓
R␈↓-mapping.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 50␈↓␈α∞In␈α∞LISP 1.5␈α∞␈↓αT␈↓␈α∂and␈α∞␈↓αF␈↓␈α∞were␈α∞used␈α∞as␈α∂the␈α∞representations␈α∞of␈α∞␈↓
t␈↓␈α∞and␈α∂␈↓
f␈↓;␈α∞the␈α∞atoms␈α∞␈↓αT␈↓␈α∞and␈α∂␈↓αF␈↓␈α∞were
␈↓ ↓H␈↓(permanently) bound to values ␈↓α*T*␈↓ and ␈↓αNIL␈↓.
␈↓ ↓H␈↓␈↓↓4.2␈↓ εzS-expr translation of LISP expressions     93␈↓


␈↓ ↓H␈↓You␈αshould␈αgo␈α
back␈αand␈αlook␈α
at␈αthe␈α␈↓αtgm␈↓'s␈α(Section 3.7)␈α
now␈αthat␈αyou␈α
know␈αthat␈αthey␈αare␈α
evaluators
␈↓ ↓H␈↓for␈α∪simple␈α∪subsets␈α∪of␈α∪LISP␈α∪expressions.␈α∪ Note␈α∪that␈α∪the␈α∪only␈α∪atoms␈α∪which␈α∪the␈α∀great␈α∪mothers
␈↓ ↓H␈↓recognize␈α∞are␈α∞␈↓αT␈↓␈α∞and␈α∞␈↓αNIL␈↓.␈α∞Any␈α∞other␈α∞atoms␈α
elicit␈α∞an␈α∞error␈α∞message.␈α∞ What␈α∞do␈α∞these␈α∞other␈α
atoms
␈↓ ↓H␈↓represent?␈α
That␈α
is,␈α
of␈α
what␈α
objects␈α
are␈α
atoms␈α
the␈α
␈↓
R␈↓-maps?␈α
Well,␈α
numerals␈α
are␈α
atoms␈α
and␈α
are␈α
the
␈↓ ↓H␈↓␈↓
R␈↓-maps␈αof␈αnumerals.␈αWe␈αcertainly␈αcould␈αextend␈α␈↓αtgmoaf␈↓␈αto␈αhandle␈αthis␈αcase.␈α Atoms␈αare␈αtranslations
␈↓ ↓H␈↓of␈αanother␈α
class␈αof␈αLISP␈α
expressions;␈αthey␈α
are␈αthe␈αtranslations␈α
of␈αvariables␈α
and␈αfunction␈αnames.␈α
 So
␈↓ ↓H␈↓one␈α
task␈αbefore␈α
us␈α
is␈αto␈α
incorporate␈αa␈α
mechanism␈α
into␈αour␈α
simple␈α
LISP␈αevaluator␈α
which␈αwill␈α
handle
␈↓ ↓H␈↓evaluation␈α⊃of␈α∩variables.␈α⊃We've␈α⊃already␈α∩seen␈α⊃the␈α⊃necessary␈α∩mechanism␈α⊃in␈α⊃Section 3.6␈α∩where␈α⊃we
␈↓ ↓H␈↓studied␈α
tables␈αas␈α
an␈αabstract␈α
data␈α
stucture.␈αThe␈α
other␈αpiece␈α
of␈α
LISP␈αwhich␈α
did␈αnot␈α
appear␈α
in␈αthe
␈↓ ↓H␈↓evaluator␈α∂for␈α⊂polynomials␈α∂was␈α∂conditional␈α⊂expressions.␈α∂Before␈α∂handling␈α⊂conditionals␈α∂we␈α⊂wish␈α∂to
␈↓ ↓H␈↓handle the informal intuitive discussion of tables in Section 3.6 in a more precise manner.



␈↓ ↓H␈↓␈↓ ¬T␈↓↓4.3  Symbol tables␈↓


␈↓ ↓H␈↓One␈α∞of␈α∞the␈α∞distinguishing␈α∞features␈α∞of␈α∞computer␈α∞science␈α∞is␈α∞its␈α∞reliance␈α∞on␈α∞the␈α∞ability␈α∞to␈α∞store␈α∞and
␈↓ ↓H␈↓recover␈αinformation.␈α Any␈αformalism␈αwhich␈αaddresses␈αitself␈αto␈αcomputer␈αscience␈αmust␈αtake␈αthis␈αinto
␈↓ ↓H␈↓account.␈α In␈α
particular␈αwe␈α
must␈αbe␈α
able␈αto␈α
handle␈αthe␈α
effect␈αof␈α
assignment␈αor␈α
binding,␈αthat␈α
is,␈αthe
␈↓ ↓H␈↓association␈αof␈αa␈αvalue␈αwith␈αa␈αname␈αin␈αan␈αenvironment.␈α A␈αcommon␈αdevice␈αused␈αto␈αaccomplish␈αthis
␈↓ ↓H␈↓is␈αthe␈αsymbol␈αtable.␈α
This␈αis␈αthe␈αdevice␈α
we␈αused␈αinformally␈αin␈α
Section 3.6.␈α We␈αwill␈αreview␈α
some␈αof
␈↓ ↓H␈↓that discussion here.

␈↓ ↓H␈↓In␈α
essence,␈α
a␈α
symbol␈αtable␈α
is␈α
simply␈α
a␈α
set␈αof␈α
ordered␈α
pairs␈α
of␈α
objects;␈αone␈α
of␈α
the␈α
elements␈α
of␈αeach
␈↓ ↓H␈↓pair␈α⊂is␈α⊂a␈α∂name;␈α⊂the␈α⊂other␈α∂is␈α⊂its␈α⊂value.␈α⊂ This␈α∂means␈α⊂that␈α⊂symbol␈α∂tables␈α⊂can␈α⊂be␈α⊂characterized␈α∂as
␈↓ ↓H␈↓relations␈α⊂or␈α∂perhaps␈α⊂even␈α⊂as␈α∂functions.␈α⊂ This␈α∂characterization␈α⊂is␈α⊂indeed␈α∂viable.␈α⊂On␈α⊂page 80␈α∂we
␈↓ ↓H␈↓showed␈αthat␈αa␈α
table␈αcould␈αbe␈αconstructed␈α
and␈αmaintained␈αin␈αa␈α
manner␈αpreserving␈αset-ness.␈α
 As␈αan
␈↓ ↓H␈↓abstract␈αoperation,␈αfinding␈αan␈αelement␈αin␈αa␈αsymbol␈αtable␈αis␈αalso␈αquite␈αsimple:␈αgiven␈αa␈αset␈αof␈α
ordered
␈↓ ↓H␈↓pairs␈αand␈αa␈αvariable,␈α
find␈αa␈αpair␈αwith␈α
first␈αelement␈αthe␈αsame␈α
as␈αthe␈αgiven␈αvariable.␈α This␈α
operation
␈↓ ↓H␈↓can␈α∂be␈α∂described␈α∂as␈α∂function␈α∂application␈α∂where␈α∞the␈α∂function␈α∂being␈α∂applied␈α∂is␈α∂the␈α∂table␈α∂and␈α∞the
␈↓ ↓H␈↓argument is the name component.  That is:    ␈↓αlocate[x;tbl] = tbl(x)␈↓.

␈↓ ↓H␈↓This␈α∀level␈α∀of␈α∀abstraction␈α∀was␈α∀a␈α∀bit␈α∀too␈α∀spartan;␈α∀maintaining␈α∀such␈α∀tables␈α∀is␈α∀computationally
␈↓ ↓H␈↓expensive,␈αand␈αwe␈αwill␈α␈↓↓have␈↓␈αto␈αgive␈αa␈α␈↓αlocate␈↓␈αalgorithm␈αsooner␈αor␈αlater.␈α The␈αlevel␈αof␈αabstraction␈αwe
␈↓ ↓H␈↓envisioned␈αlooked␈αon␈αa␈αsymbol␈αtable␈αas␈αa␈α␈↓↓sequence␈↓␈αof␈αpairs,␈αeach␈αpair␈αrepresenting␈αa␈αvariable␈αand
␈↓ ↓H␈↓its␈α∂corresponding␈α∂value.␈α∂ The␈α∂algorithms␈α⊂given␈α∂in␈α∂Section 3.6␈α∂for␈α∂manipulating␈α⊂tables␈α∂depended
␈↓ ↓H␈↓heavily␈α∞on␈α∞the␈α∞implied␈α
sequencing␈α∞of␈α∞call-by-value␈α∞and␈α
recursion.␈α∞ Since␈α∞this␈α∞was␈α∞consistent␈α
with
␈↓ ↓H␈↓the␈α∞explicit␈α∞sequencing␈α∞used␈α∞in␈α∞adding␈α∞elements␈α
to␈α∞the␈α∞table,␈α∞we␈α∞achieved␈α∞the␈α∞desired␈α∞effect.␈α
We
␈↓ ↓H␈↓found␈αthe␈αexpected␈αbindings,␈αeven␈αthough␈αthere␈α
may␈αhave␈αbeen␈αother␈αcandidates␈αin␈αthe␈α
tables.␈αIn
␈↓ ↓H␈↓the␈α∩remaining␈α⊃sections␈α∩of␈α⊃this␈α∩chapter␈α∩we␈α⊃will␈α∩utilize␈α⊃more␈α∩features␈α⊃of␈α∩this␈α∩interplay␈α⊃between
␈↓ ↓H␈↓representation␈αof␈αdata␈αand␈αcalling␈αstyle␈αof␈αalgorithm.␈αSymbol␈αtables␈αare␈αjust␈αthe␈αfirst␈αmanifestation
␈↓ ↓H␈↓of this phenomenon.

␈↓ ↓H␈↓These␈α
simple␈α
symbol␈α
tables␈αare␈α
also␈α
known␈α
as␈αassociation␈α
lists␈α
or␈α
␈↓↓a-lists␈↓;␈αthus␈α
␈↓αassoc␈↓␈α
will␈α
be␈αthe␈α
name
␈↓ ↓H␈↓␈↓↓94  Evaluation␈↓ 24.3␈↓


␈↓ ↓H␈↓of␈αour␈αfunction␈α
to␈αsearch␈αa␈α
symbol␈αtable.␈α␈↓αassoc␈↓␈α
will␈αtake␈αtwo␈α
arguments:␈αa␈αname␈α
and␈αa␈αsymbol␈α
table.
␈↓ ↓H␈↓It␈α
will␈αexamine␈α
the␈α
table␈αfrom␈α
left␈α
to␈αright,␈α
looking␈αfor␈α
the␈α
first␈αmatch.␈α
 We␈α
will␈αneed␈α
to␈αdesignate␈α
a
␈↓ ↓H␈↓selector,␈α
␈↓αname␈↓,␈α
to␈αlocate␈α
the␈α
name-component␈αof␈α
a␈α
pair,␈αand␈α
another␈α
selector,␈α␈↓αvalue␈↓,␈α
to␈α
retrieve␈αthe
␈↓ ↓H␈↓value␈αcomponent.␈α If␈α
a␈αpair␈αis␈αfound,␈α
then␈αthat␈αpair␈αis␈α
returned;␈αif␈α␈↓↓no␈↓␈αsuch␈α
pair␈αis␈αfound,␈αthe␈α
result
␈↓ ↓H␈↓is undefined.

␈↓ ↓H␈↓α␈↓ αXassoc[x;l] <=␈↓ ∧H[eq[name[first[l]];x] → first[l];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H ␈↓
t␈↓α → assoc[x;rest[l]]]

␈↓ ↓H␈↓Obviously,␈α
if␈α
the␈αtable␈α
is␈α
very␈αlong␈α
and␈α
the␈α
desired␈αpair␈α
is␈α
close␈αto␈α
the␈α
end␈αof␈α
the␈α
table,␈α
then␈αwe
␈↓ ↓H␈↓may␈α
be␈α
in␈α
for␈α
a␈α
very␈α
long␈α
search.␈α
The␈α
search␈α
scheme␈α
encoded␈α
in␈α
␈↓αassoc␈↓␈α
is␈α
called␈α
␈↓↓linear␈α
search␈↓,␈α
and␈α
is
␈↓ ↓H␈↓unnecessarily␈α⊂inefficient.␈α⊃However␈α⊂the␈α⊃phenomemona␈α⊂we␈α⊃wish␈α⊂to␈α⊃study␈α⊂here␈α⊃are␈α⊂not␈α⊃related␈α⊂to
␈↓ ↓H␈↓efficiency␈α∞of␈α∞searching␈α∞methods.␈α∞ We␈α∞will␈α∞come␈α
back␈α∞to␈α∞symbol␈α∞tables␈α∞in␈α∞Section 5.5␈α∞to␈α∞study␈α
the
␈↓ ↓H␈↓problems␈αof␈αefficient␈α
storage␈αand␈αretrieval␈α
of␈αinformation.␈αIt␈α
will␈αsuffice␈αnow␈α
simply␈αto␈αthink␈α
of␈αa
␈↓ ↓H␈↓symbol␈α
table␈α
as␈α
represented␈α
in␈α∞LISP␈α
by␈α
a␈α
list␈α
of␈α
dotted␈α∞pairs,␈α
a␈α
name␈α
dotted␈α
with␈α
value.␈α∞ In␈α
this
␈↓ ↓H␈↓representation,␈α∞then,␈α∞␈↓αname[x]␈α∞<=␈α∞car[x]␈↓,␈α∞and␈α∞␈↓αvalue[x] <= cdr[x]␈↓.␈α∞ For␈α∞completeness,␈α∞we␈α∂should␈α∞also
␈↓ ↓H␈↓specify␈αa␈α
constructor.␈αThough␈α
we␈αwon't␈α
need␈αit␈α
for␈αa␈α
while,␈αits␈α
name␈αis␈α
␈↓αmkent␈↓,␈αand␈α
will␈αtake␈αa␈α
name
␈↓ ↓H␈↓and a value and return a new entry. Its representation here is ␈↓αmkent[x;y] <= cons[x;y]␈↓.

␈↓ ↓H␈↓Recall␈α
that␈α
we␈α
are␈α
representing␈α
variables␈α∞as␈α
atoms;␈α
if␈α
␈↓αx,␈α
y,␈α
␈↓and␈α∞␈↓αz␈↓␈α
had␈α
current␈α
values␈α
␈↓α2,␈α
3,␈α∞␈↓and␈α
␈↓α4␈↓,
␈↓ ↓H␈↓then a symbol table describing that fact could be encoded as:

␈↓ ↓H␈↓␈↓ ¬3␈↓α((X . 2) (Y . 3) (Z . 4)) .␈↓ 

␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓␈↓ ∧B␈↓αassoc[Y; ((X . 2) (Y . 3) (Z . 4))] = (Y . 3)
␈↓ ↓H␈↓α␈↓ ∧Xassoc[U; ((X . 2) (Y . 3) (Z . 4))] ␈↓ = ␈↓λB␈↓.


␈↓ ↓H␈↓How␈αare␈αwe␈αto␈αrepresent␈αbindings␈αof␈αvariables␈αto␈αnon-numeric␈αS-exprs?␈α For␈αexample,␈αhow␈αwill␈αwe
␈↓ ↓H␈↓represent:␈α
"the␈α
current␈α
value␈α
of␈α
␈↓αx␈↓␈α
is␈α
␈↓αA␈↓"?␈α
 We␈αwill␈α
place␈α
the␈α
dotted-pair␈α
␈↓α(X␈α
.␈α
A)␈↓␈α
in␈α
the␈α
table.␈αNow
␈↓ ↓H␈↓this␈α
representation␈α
is␈α
certainly␈α
open␈α
to␈α
question:␈αwhy␈α
not␈α
add␈α
␈↓α(X␈α
.(QUOTE␈α
A))␈↓?␈α
 The␈αlatter␈α
notation
␈↓ ↓H␈↓is␈α∞more␈α∂consistent␈α∞with␈α∞our␈α∂conception␈α∞of␈α∞representation␈α∂espoused␈α∞on␈α∞page 55.␈α∂ That␈α∞is,␈α∂we␈α∞map
␈↓ ↓H␈↓LISP␈α⊃expressions␈α⊃to␈α∩S-expressions;␈α⊃perform␈α⊃the␈α⊃calculations␈α∩on␈α⊃this␈α⊃representation,␈α∩and␈α⊃finally
␈↓ ↓H␈↓␈↓↓reinterpret␈↓␈αthe␈αresult␈αof␈αthis␈αcalculation␈αas␈αa␈αLISP␈αexpression.␈αThe␈αrepresentation␈αwe␈αhave␈αchosen
␈↓ ↓H␈↓for␈α∞symbol␈α∞tables␈α∞obviates␈α∞the␈α
last␈α∞reinterpretation␈α∞step.␈α∞Now␈α∞it␈α
will␈α∞turn␈α∞out␈α∞that␈α∞for␈α∞our␈α
initial
␈↓ ↓H␈↓subsets␈αof␈αLISP␈αthis␈αreinterpretation␈αstep␈αsimply␈αwould␈αinvolve␈α"stripping"␈αthe␈α␈↓αQUOTE␈↓s.␈α The␈α
only
␈↓ ↓H␈↓"values"␈α⊂which␈α⊂a␈α⊂computation␈α⊂can␈α⊂return␈α∂are␈α⊂constants;␈α⊂later␈α⊂things␈α⊂will␈α⊂become␈α⊂more␈α∂difficult.
␈↓ ↓H␈↓Perhaps␈α∂this␈α∂representation␈α∂of␈α∂table␈α∂entries␈α∂is␈α∂a␈α∞poor␈α∂one;␈α∂we␈α∂will␈α∂see.␈α∂In␈α∂studying␈α∂any␈α∞existing
␈↓ ↓H␈↓language,␈α⊗or␈α↔contemplating␈α⊗the␈α↔design␈α⊗of␈α↔any␈α⊗new␈α⊗one,␈α↔we␈α⊗must␈α↔question␈α⊗each␈α↔detail␈α⊗of
␈↓ ↓H␈↓representation. Decisions made too early can have serious consequences␈↓π 51␈↓.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 51␈↓␈αHowever,␈αin␈αdefense␈αof␈αLISP,␈αit␈αmust␈αbe␈αremembered␈αthat␈αLISP␈αwas␈αconceived␈αat␈αthe␈αtime␈αthat
␈↓ ↓H␈↓FORTRAN␈α
was␈α
believed␈αto␈α
be␈α
a␈αgift␈α
from␈α
God.␈αOnly␈α
later␈α
did␈αwe␈α
learn␈α
the␈αtrue␈α
identity␈α
of␈αthe
␈↓ ↓H␈↓donor.
␈↓ ↓H␈↓␈↓↓4.3␈↓ 	KSymbol tables     95␈↓


␈↓ ↓H␈↓Before␈α
moving␈α
on␈α
we␈αshould␈α
probably␈α
take␈α
stock␈αof␈α
our␈α
current␈α
position;␈αin␈α
this␈α
section␈α
we␈αhave
␈↓ ↓H␈↓simply␈αrecreated␈αthe␈αtable-lookup␈αmechanism␈αwe␈αused␈αin␈αSection 3.6,␈αonly␈αnow␈αwe␈αare␈αpaying␈αa␈αbit
␈↓ ↓H␈↓more␈α∞attention␈α∞to␈α∞representation.␈α∞We␈α∂can␈α∞locate␈α∞things␈α∞in␈α∞a␈α∂table␈α∞and␈α∞we␈α∞have␈α∞seen␈α∂how␈α∞calling
␈↓ ↓H␈↓functions␈α
can␈αadd␈α
values␈αto␈α
a␈αtable.␈α
We␈α
have␈αsaid␈α
nothing␈αabout␈α
adding␈αfunction␈α
definitions␈αto␈α
the
␈↓ ↓H␈↓tables.␈α
 Abstractly␈α
we␈αknow␈α
how␈α
to␈αextract␈α
the␈α
definition␈α
from␈αthe␈α
table␈α
and␈αapply␈α
it.␈α
We␈αmust␈α
give
␈↓ ↓H␈↓an␈α
explicit␈αrepresentation␈α
of␈αthe␈α
storage␈αof␈α
a␈α
function.␈αThis␈α
turns␈αout␈α
to␈αbe␈α
a␈αreasonably␈α
non-trivial
␈↓ ↓H␈↓problem.␈α⊂ We␈α∂have␈α⊂seen␈α⊂that␈α∂it␈α⊂is␈α⊂possible␈α∂to␈α⊂mechanize␈α∂at␈α⊂least␈α⊂one␈α∂scheme␈α⊂for␈α⊂evaluation␈α∂of
␈↓ ↓H␈↓functions␈α--␈αcall-by-value,␈αevaluating␈αarguments␈αfrom␈αleft␈αto␈αright.␈α We␈αhave␈αseen␈αthat␈αit␈αis␈αpossible
␈↓ ↓H␈↓to␈αtranslate␈αLISP␈αexpressions␈αinto␈αS-exprs␈αin␈αsuch␈αa␈αway␈αthat␈αwe␈αcan␈αwrite␈αa␈αLISP␈αfunction␈αwhich
␈↓ ↓H␈↓will␈α⊃act␈α⊃as␈α⊃an␈α⊃evaluator␈α⊃for␈α⊃such␈α⊃translations.␈α⊃ In␈α⊃the␈α⊃process␈α⊃we␈α⊃have␈α⊃had␈α⊃to␈α⊃mechanize␈α⊂the
␈↓ ↓H␈↓intuitive␈α∞devices␈α
we␈α∞(as␈α
humans)␈α∞might␈α
use␈α∞to␈α
recall␈α∞the␈α
definition␈α∞of␈α
functions␈α∞and␈α
to␈α∞recall␈α
the
␈↓ ↓H␈↓current␈α∞values␈α∞of␈α
variables.␈α∞ It␈α∞became␈α
clear␈α∞that␈α∞the␈α
mechanism␈α∞of␈α∞symbol␈α
tables␈α∞could␈α∞be␈α
used.
␈↓ ↓H␈↓To␈α∞associate␈α
a␈α∞variable␈α∞with␈α
a␈α∞value␈α∞was␈α
easy.␈α∞ To␈α
associate␈α∞a␈α∞function␈α
name␈α∞with␈α∞its␈α
definition
␈↓ ↓H␈↓required␈αsome␈αcare.␈α That␈αis,␈αpart␈αof␈αthe␈α
definition␈αof␈αa␈αfunction␈αinvolves␈αthe␈αproper␈αassociation␈α
of
␈↓ ↓H␈↓formal␈αparameters␈αwith␈αthe␈αbody␈αof␈αthe␈αdefinition.␈α(We␈αactually␈αneed␈αto␈αbe␈αa␈αbit␈αmore␈αprecise␈αthan
␈↓ ↓H␈↓this␈α∞in␈α∞describing␈α∞recursive␈α
functions,␈α∞but␈α∞this␈α∞is␈α
good␈α∞enough␈α∞for␈α∞now.)␈α
The␈α∞device␈α∞we␈α∞chose␈α
is
␈↓ ↓H␈↓called the ␈↓↓lambda notation␈↓.



␈↓ ↓H␈↓␈↓ ¬l␈↓↓4.4  ␈↓αλ␈↓↓-notation␈↓α


␈↓ ↓H␈↓Recall␈α⊂our␈α⊂discussion␈α∂of␈α⊂the␈α⊂problems␈α∂of␈α⊂representation␈α⊂of␈α∂function␈α⊂definitions.␈α⊂This␈α∂discussion
␈↓ ↓H␈↓began␈α
on␈α
page 76␈α
and␈α
our␈α
conclusion␈α
was␈α
that␈α
to␈α
represent␈α
a␈α
definition␈α
like␈α
␈↓αf[x;y] <= ␈↓λx␈↓␈α
we␈α
needed␈α
a
␈↓ ↓H␈↓symbol␈α
table␈α
entry␈α
with␈α
name␈α␈↓αf␈↓␈α
and␈α
a␈α
value␈α
part␈α
which␈αcontained␈α
the␈α
body␈α
of␈α
the␈α
definition,␈α␈↓λx␈↓,␈α
and
␈↓ ↓H␈↓the␈α
list␈α
of␈α
arguments,␈α
␈↓α[x;y]␈↓,␈α
given␈α
with␈α
␈↓αf␈↓.␈α
 LISP␈α
uses␈α
the␈α
λ-notation␈α
to␈α
lend␈α
precision␈α
to␈αour␈α
informal
␈↓ ↓H␈↓discussion of function representation.

␈↓ ↓H␈↓Why␈α⊂add␈α⊂more␈α∂notation␈α⊂to␈α⊂LISP?␈α∂The␈α⊂λ-notation␈α⊂is␈α∂a␈α⊂device␈α⊂invented␈α∂by␈α⊂the␈α⊂logician␈α∂Alonzo
␈↓ ↓H␈↓Church␈αin␈αconjunction␈αwith␈α
his␈αstudies␈αin␈αlogic␈αand␈α
foundations␈αof␈αmathematics.␈α The␈αλ-calculus␈α
is
␈↓ ↓H␈↓useful␈α
for␈αa␈α
careful␈αdiscussion␈α
of␈αfunctions␈α
and␈αis␈α
therefore␈αapplicable␈α
in␈αa␈α
purified␈α
discussion␈αof
␈↓ ↓H␈↓procedures␈α
in␈αprogramming␈α
languages.␈α
We␈αshall␈α
begin␈αa␈α
detailed␈α
discussion␈αof␈α
the␈α
λ-calculus␈αand
␈↓ ↓H␈↓its relation to computer science in Section .

␈↓ ↓H␈↓The␈α
notation␈αwas␈α
introduced␈α
into␈αComputer␈α
Science␈α
by␈αJohn␈α
McCarthy␈α
in␈αthe␈α
description␈αof␈α
LISP.
␈↓ ↓H␈↓In␈α∂the␈α∞interest␈α∂of␈α∂precision␈α∞we␈α∂should␈α∂note␈α∞that␈α∂there␈α∂are␈α∞actually␈α∂several␈α∂important␈α∞distinctions
␈↓ ↓H␈↓between␈α⊂Church's␈α⊃λ-calculus␈α⊂and␈α⊂the␈α⊃notation␈α⊂envisioned␈α⊃by␈α⊂McCarthy.␈α⊂We␈α⊃will␈α⊂point␈α⊃out␈α⊂the
␈↓ ↓H␈↓discrepancies in Section .

␈↓ ↓H␈↓We␈αhave␈α
been␈αinformally␈α
writing␈α␈↓αf[x;y] <= [x*y + y]␈↓␈α
as␈αa␈α
definition␈αof␈α
the␈αfunction␈α
␈↓αf␈↓.␈αIt␈αis␈α
supposed
␈↓ ↓H␈↓to␈αconvey␈αthe␈αfollowing␈αintent:␈α␈↓αf␈↓␈α
is␈αthe␈αname␈αof␈αa␈αfunction␈α
or␈αrule;␈αwhenever␈α␈↓αf␈↓␈αis␈αsupplied␈αwith␈α
two
␈↓ ↓H␈↓numeric␈α∞arguments␈α∞it␈α∞is␈α∞supposed␈α∞to␈α∞multiply␈α∞those␈α∞arguments␈α∞and␈α∞add␈α∞the␈α∞result␈α∞to␈α∂the␈α∞second.
␈↓ ↓H␈↓The␈α∞resulting␈α∞sum␈α∞is␈α∞the␈α∞desired␈α∂answer.␈α∞ Since␈α∞informality␈α∞has␈α∞tendencies␈α∞toward␈α∂ambiguity␈α∞we
␈↓ ↓H␈↓would␈αlike␈αto␈αanalyze␈αthe␈α"<="-notation␈αmore␈αclosely.␈αThough␈αwe␈αsay␈α␈↓αf␈↓␈αis␈αbeing␈αdefined,␈αit␈αis␈αnot␈α␈↓αf␈↓,
␈↓ ↓H␈↓␈↓↓96  Evaluation␈↓ /4.4␈↓


␈↓ ↓H␈↓but␈α␈↓αf[x;y]␈↓␈αwhich␈α
appears␈αto␈αthe␈αleft␈α
of␈αthe␈α"<="-symbol.␈α First␈α
note␈αthat␈α␈↓αf[x;y]␈↓␈αis␈α
␈↓↓not␈↓␈αa␈αfunction,␈α␈↓αf␈↓␈α
is.
␈↓ ↓H␈↓To␈α∪see␈α∪what␈α∪␈↓αf[x;y]␈↓␈α∪means␈α∪consider␈α∪the␈α∪following␈α∪example.␈α∪ When␈α∪we␈α∪are␈α∪asked␈α∪to␈α∩evaluate
␈↓ ↓H␈↓␈↓αcar[(A . B)]␈↓␈αwe␈αsay␈αthe␈αvalue␈αis␈α␈↓αA␈↓.␈α ␈↓αcar[(A . B)]␈↓␈αis␈αan␈αexpression␈αto␈αbe␈αevaluated;␈αit␈αis␈αa␈αLISP␈αform.
␈↓ ↓H␈↓If␈α␈↓αcar[(A . B)]␈↓␈α␈↓↓is␈↓␈α
a␈αform␈αthen␈αso␈α
is␈α␈↓αcar[x]␈↓;␈αonly␈αnow␈α
the␈αvalue␈αof␈αthe␈α
form␈αdepends␈αon␈α
the␈αcurrent
␈↓ ↓H␈↓value␈α∞assigned␈α∞to␈α∞the␈α
variable␈α∞␈↓αx␈↓.␈α∞ So␈α∞the␈α∞␈↓↓function␈↓␈α
is␈α∞␈↓αcar␈↓;␈α∞the␈α∞␈↓↓form␈↓␈α
is␈α∞␈↓αcar[x]␈↓.␈α∞ The␈α∞function␈α∞is␈α
␈↓αf␈↓;
␈↓ ↓H␈↓␈↓αf[x;y]␈↓␈αis␈αa␈αform,␈αas␈αis␈α␈↓αx*y + y␈↓.␈α Thus␈αthe␈αcurrent␈αnotation␈αhas␈αa␈αform␈αon␈αboth␈αsides␈αof␈αthe␈α"<=".␈αWe
␈↓ ↓H␈↓would like a notation which clearly shows what is being defined and what is given.

␈↓ ↓H␈↓Also␈αour␈αnotation␈α
has␈αreally␈αbeen␈αspecifying␈α
more␈αthan␈αjust␈αthe␈α
name.␈α The␈αnotation␈α
specifies␈αthe
␈↓ ↓H␈↓formal␈αparameters␈α(␈↓αx␈↓␈αand␈α␈↓αy␈↓)␈αand␈αthe␈αorder␈αin␈αwhich␈αwe␈αare␈αto␈αassociate␈αactual␈αparameters␈αin␈αa␈αcall
␈↓ ↓H␈↓with␈αthe␈αformal␈αparameters␈αof␈αthe␈αdefinition␈α(␈↓αx␈↓␈αwith␈αthe␈αfirst,␈α␈↓αy␈↓␈αwith␈αthe␈αsecond).␈α More␈αsubtly,␈αthe
␈↓ ↓H␈↓notation␈αtells␈α␈↓↓which␈↓␈αvariables␈αin␈αthe␈αfunction␈αbody␈αare␈αto␈αbe␈αsupplied␈αvalues␈αwhen␈αthe␈αfunction␈αis
␈↓ ↓H␈↓called.␈αFor␈αexample␈αdefine␈α␈↓αg[x] <= [x*y + y]␈↓;␈α
then␈αthe␈αexpression␈α␈↓αg[2]␈↓␈αspecifies␈α
that␈α␈↓αx␈↓␈αis␈αto␈αreceive␈α
a
␈↓ ↓H␈↓value ␈↓α2␈↓, but leaves unspecified what the value of ␈↓αy␈↓, and indeed + and *, should be.

␈↓ ↓H␈↓We␈αalso␈αwish␈αto␈αhave␈αa␈αnotation␈αso␈αthat␈αfunction␈αdefinitions␈αcan␈αbe␈αinserted␈αinto␈αthe␈αsymbol␈αtable
␈↓ ↓H␈↓as␈α⊃"values"␈α⊃assigned␈α⊃to␈α⊃names.␈α⊃They␈α⊃will␈α⊃be␈α⊃parametric␈α⊃values,␈α⊃but␈α⊃they␈α⊃will␈α⊃be␈α∩values.␈α⊃ The
␈↓ ↓H␈↓λ-notation␈α
performs␈αthis␈α
task␈αby␈α
preceding␈α
the␈αfunction␈α
body␈αwith␈α
a␈α
list␈αof␈α
variables,␈αcalled␈α
␈↓↓lambda
␈↓ ↓H␈↓↓variables␈↓␈↓π 52␈↓;␈α
The␈αresulting␈α
construct␈α
is␈αpreceded␈α
by␈α
"λ["␈αand␈α
followed␈α
by␈α"]".␈α
 Thus␈α
using␈αthe␈α
above
␈↓ ↓H␈↓example,␈α
the␈α
name␈α
␈↓αf␈↓␈α
is␈αexactly␈α
the␈α
same␈α
function␈α
as␈α␈↓αλ[[x;y] x*y + y]␈↓.␈α
We␈α
actually␈α
need␈α
a␈α
bit␈αmore
␈↓ ↓H␈↓than␈α↔λ-notation␈α↔to␈α⊗specify␈α↔recursive␈α↔functions␈α⊗in␈α↔a␈α↔natural␈α⊗manner.␈α↔See␈α↔Section 4.9.␈α⊗ The
␈↓ ↓H␈↓λ-notation␈α∂introduces␈α∂nothing␈α⊂new␈α∂as␈α∂far␈α⊂as␈α∂our␈α∂intuitive␈α⊂binding␈α∂and␈α∂evaluation␈α⊂processes␈α∂are
␈↓ ↓H␈↓concerned; it only makes these operations more clear.

␈↓ ↓H␈↓One␈α∂benefit␈α∂of␈α∂the␈α⊂λ-notation␈α∂is␈α∂that␈α∂we␈α⊂need␈α∂not␈α∂give␈α∂explicit␈α⊂names␈α∂to␈α∂functions␈α∂in␈α⊂order␈α∂to
␈↓ ↓H␈↓perform␈αthe␈αevaluation.␈αEvaluation␈αof␈αsuch␈αanonymous␈αfunctions␈αshould␈αbe␈αwithin␈αthe␈αprovince␈αof
␈↓ ↓H␈↓LISP.␈α
 To␈αevaluate␈α
a␈α
λ-expression␈αin␈α
LISP,␈α
bind␈αthe␈α
evaluated␈α
actual␈αparameters␈α
to␈αthe␈α
λ-variables
␈↓ ↓H␈↓and evaluate the function body. For example, evaluate:

␈↓ ↓H␈↓␈↓ ¬O␈↓αλ[[x;y] x␈↓π2␈↓α + y][2;3] ␈↓. 

␈↓ ↓H␈↓We associate ␈↓α2␈↓ with ␈↓αx␈↓ and ␈↓α3␈↓ with ␈↓αy␈↓ and evaluate the expression:

␈↓ ↓H␈↓␈↓ ε(␈↓αx␈↓π2␈↓α + y␈↓. 

␈↓ ↓H␈↓This calculation should give ␈↓α7␈↓ as value.

␈↓ ↓H␈↓Or evaluate: ␈↓ ¬␈↓αλ[[x] cdr[car[x]]][((A . B). C)]␈↓. 

␈↓ ↓H␈↓We␈α∩bind␈α⊃␈↓αx␈↓␈α∩to␈α⊃the␈α∩S-expression␈α⊃␈↓α((A . B). C)␈↓␈α∩and␈α⊃evaluate␈α∩the␈α⊃function␈α∩body.␈α⊃The␈α∩usual␈α⊃LISP
␈↓ ↓H␈↓evaluation␈α∂scheme␈α∂entails␈α∞evaluating␈α∂␈↓αcar[x]␈↓␈α∂with␈α∂the␈α∞current␈α∂binding␈α∂of␈α∞␈↓αx␈↓;␈α∂this␈α∂result,␈α∂␈↓α(A . B)␈↓,␈α∞is
␈↓ ↓H␈↓passed to ␈↓αcdr␈↓.  ␈↓αcdr␈↓ performs its calculation and finally returns ␈↓αB␈↓.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 52␈↓ The list of variables is usually referred to as the ␈↓↓lambda list␈↓.
␈↓ ↓H␈↓␈↓↓4.4␈↓ 	}␈↓αλ␈↓↓-notation     97␈↓α


␈↓ ↓H␈↓The␈α
λ-notation␈α
can␈α
be␈α
used␈α∞anywhere␈α
LISP␈α
expects␈α
to␈α
find␈α∞a␈α
function,␈α
thus␈α
allowing␈α
us␈α∞to␈α
write
␈↓ ↓H␈↓anonymous functions.  For example:

␈↓ ↓H␈↓␈↓ ∧s␈↓αλ[[x] first[x]][λ[[y] rest[y]][(A B)]]␈↓ 

␈↓ ↓H␈↓This expression is a complicated way of writing:

␈↓ ↓H␈↓␈↓ βz␈↓αf[g[(A B)]]␈↓ where ␈↓αf[x] <= first[x] ␈↓and ␈↓αg[y] <= rest[y]␈↓. 

␈↓ ↓H␈↓Though␈αthe␈αsecond␈αform␈αis␈αperhaps␈αeasier␈αfor␈αus␈αto␈αcomprehend,␈αthe␈αfirst␈αform␈α␈↓↓is␈↓␈α
equivalent␈αand
␈↓ ↓H␈↓will␈α
be␈α
acceptable␈α
to␈α
the␈α
evaluator␈α
we␈α∞will␈α
write.␈α
Indeed␈α
the␈α
mechanical␈α
evaluation␈α
of␈α∞the␈α
second
␈↓ ↓H␈↓formulation will pass through the first on its way to complete evaluation. At any rate:

␈↓ ↓H␈↓␈↓ βO␈↓αλ[[x] first[x]][λ[[y] rest[y]][(A B)]] = λ[[x] first[x]][(B)] = B␈↓ 

␈↓ ↓H␈↓Still,␈α∂evaluation␈α∂requires␈α∂care.␈α∂For␈α∂example,␈α⊂is␈α∂␈↓αλ[[x]2]␈↓␈α∂the␈α∂constant␈α∂function␈α∂which␈α⊂always␈α∂gives
␈↓ ↓H␈↓value␈α∂␈↓α2␈↓?␈α∂It␈α∂isn't␈α∂in␈α∞LISP.␈α∂The␈α∂evaluation␈α∂of␈α∂an␈α∞expression␈α∂involving␈α∂this␈α∂function␈α∂requires␈α∞the
␈↓ ↓H␈↓evaluation␈α∞of␈α∞the␈α∞actual␈α∞parameter␈α∞associated␈α∂with␈α∞␈↓αx␈↓.␈α∞ That␈α∞computation␈α∞may␈α∞not␈α∂terminate.␈α∞For
␈↓ ↓H␈↓example,␈αconsider␈α
␈↓αλ[[x]2][fact[-1]]␈↓␈αwhere␈α
␈↓αfact␈↓␈αis␈αthe␈α
LISP␈αimplementation␈α
of␈αthe␈α
factorial␈αfunction
␈↓ ↓H␈↓given on page 46.

␈↓ ↓H␈↓Since␈αwe␈αintend␈αto␈αinclude␈αλ-expressions␈αin␈αour␈αlanguage␈αwe␈αmust␈αinclude␈αan␈α␈↓
R␈↓-mapping␈α
of␈αthem
␈↓ ↓H␈↓into S-expression form:

␈↓ ↓H␈↓␈↓ βm␈↓
R␈↓∞( ␈↓αλ[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α]␈↓λx␈↓α] ␈↓∞)␈↓α = (LAMBDA (X␈↓β1␈↓α ...  X␈↓βn␈↓α) ␈↓
R␈↓∞(  ␈↓λx␈↓α ␈↓∞)␈↓α)␈↓

␈↓ ↓H␈↓Thus␈α⊂the␈α⊂character␈α⊂λ␈α⊂will␈α⊂be␈α⊂translated␈α⊂to␈α⊂␈↓αLAMBDA␈↓.␈α⊂␈↓αLAMBDA␈↓␈α⊂is␈α⊂therefore␈α⊂a␈α⊂kind␈α⊂of␈α⊂prefix
␈↓ ↓H␈↓operator␈α∪(like␈α∀␈↓αCOND␈↓)␈α∪which␈α∪will␈α∀help␈α∪the␈α∀evaluator␈α∪recognize␈α∪the␈α∀occurrence␈α∪of␈α∀a␈α∪function
␈↓ ↓H␈↓definition␈α(just␈αas␈α␈↓αCOND␈↓␈αwill␈αbe␈αused␈αby␈αthe␈αevaluator␈αto␈αrecognize␈αthe␈αoccurrence␈αof␈αa␈αtranslation
␈↓ ↓H␈↓of a conditional expression).

␈↓ ↓H␈↓Here are some examples of ␈↓αλ␈↓-expressions and their ␈↓
R␈↓-translations:

␈↓ ↓H␈↓␈↓ β≡␈↓
R␈↓∞( ␈↓αλ[[x;y] x␈↓π2␈↓α + y] ␈↓∞)␈↓α  = (LAMBDA (X Y) (PLUS (EXPT X 2) Y))
␈↓ ↓H␈↓α␈↓ βλ␈↓
R␈↓∞( ␈↓αλ[[x;y] cons[car[x];y]] ␈↓∞)␈↓α  = (LAMBDA (X Y) (CONS (CAR X) Y))

␈↓ ↓H␈↓To␈α
make␈α
our␈α
discussion␈αof␈α
λ-expressions␈α
completely␈α
legitimate,␈αour␈α
LISP␈α
syntax␈α
equations␈αshould
␈↓ ↓H␈↓now be augmented to include:

␈↓ ↓H␈↓<function>␈↓ αh::= λ[<varlist><form>]

␈↓ ↓H␈↓<varlist>␈↓ αh::= [<variable>; ... ; <variable>]
␈↓ ↓H␈↓␈↓ αh::= [ ]
␈↓ ↓H␈↓␈↓↓98  Evaluation␈↓ /4.4␈↓


␈↓ ↓H␈↓Besides␈α∞giving␈α∞a␈α∞clear␈α∞notation␈α∞for␈α∂function␈α∞definitions,␈α∞the␈α∞λ-notation␈α∞is␈α∞a␈α∂useful␈α∞computational
␈↓ ↓H␈↓device.

␈↓ ↓H␈↓Consider the following sketch of a function definition:

␈↓ ↓H␈↓␈↓ ∧e␈↓αg <= λ[[x][␈↓λp␈↓α[lic[x]] → lic[x]; .... x ...]]␈↓, 

␈↓ ↓H␈↓where␈α␈↓αlic␈↓␈αmay␈αbe␈αa␈α␈↓l␈↓ong␈α␈↓i␈↓nvolved␈α␈↓c␈↓alculation.␈α We␈αcertainly␈αmust␈αcompute␈α␈↓αlic[x]␈↓␈α␈↓↓once␈↓.␈αBut␈αas␈α
␈↓αg␈↓␈αis
␈↓ ↓H␈↓defined,␈αwe␈αwould␈αcompute␈α␈↓αlic[x]␈↓␈α␈↓↓twice␈↓␈αif␈αp␈↓β1␈↓␈αis␈αtrue:␈αonce␈αin␈αthe␈αcalculation␈αof␈αp␈↓β1␈↓,␈αand␈αonce␈αas␈αe␈↓β1␈↓.
␈↓ ↓H␈↓Since␈α
both␈αcalculations␈α
of␈α
␈↓αlic[x]␈↓␈αwill␈α
give␈α
the␈αsame␈α
value␈↓π 53␈↓,␈α
this␈αsecond␈α
calculation␈α
is␈αunnecessary.␈α
␈↓αg␈↓
␈↓ ↓H␈↓is inefficient. We could write:

␈↓ ↓H␈↓␈↓ ¬N␈↓αg <= λ[[x]f[lic[x];x]]␈↓ 

␈↓ ↓H␈↓where:

␈↓ ↓H␈↓␈↓ ¬
␈↓αf <= λ[[u;v][␈↓λp␈↓α[u] → u; .... v ...]]␈↓. 

␈↓ ↓H␈↓In␈αthis␈αscheme␈α␈↓αlic␈↓␈α␈↓↓will␈↓␈αonly␈αbe␈αevaluated␈αonce;␈αits␈αvalue␈αwill␈αbe␈αpassed␈αinto␈α␈↓αf␈↓.␈α Using␈αλ-expressions,
␈↓ ↓H␈↓in␈α
a␈α
style␈α∞called␈α
␈↓↓internal␈α
lambdas␈↓␈α∞we␈α
can␈α
improve␈α∞␈↓αg␈↓␈α
without␈α
adding␈α∞any␈α
new␈α
function␈α∞names␈α
to
␈↓ ↓H␈↓our symbol tables as follows:

␈↓ ↓H␈↓Replace the body of ␈↓αg␈↓ with:

␈↓ ↓H␈↓␈↓	LAM␈↓ ¬	 ␈↓αλ[[y][␈↓λp␈↓α[y] → y; ... x ...]][lic[x]]␈↓. 

␈↓ ↓H␈↓Call this new function ␈↓αg'␈↓:
␈↓ ↓H␈↓␈↓ αX␈↓αg' <= λ[[x] λ[[y][␈↓λp␈↓α[y] → y; ... x ... ]][lic[x]] ] ␈↓.

␈↓ ↓H␈↓Now␈α
when␈α
␈↓αg'␈↓␈α
is␈α
called␈α
we␈α
evaluate␈α
the␈αactual␈α
parameter,␈α
binding␈α
it␈α
to␈α
␈↓αx␈↓,␈α
as␈α
always;␈α
and␈αevaluate␈α
the
␈↓ ↓H␈↓body,␈α
␈↓	LAM␈↓.␈α
Evaluation␈α
of␈α
␈↓	LAM␈↓␈α
involves␈α
calculation␈α
of␈α
␈↓αlic[x]␈↓␈α
␈↓↓once␈↓,␈α
binding␈α
the␈α
result␈α
to␈α
␈↓αy␈↓.␈α
We␈α
then
␈↓ ↓H␈↓evaluate␈αthe␈αbody␈α
of␈αthe␈αconditional␈α
expression␈αas␈αbefore.␈α If␈α
p␈↓β1␈↓␈α␈↓↓is␈↓␈αtrue,␈α
then␈αthis␈αdefinition␈α
of␈α␈↓αg'␈↓
␈↓ ↓H␈↓involves␈α
one␈α
calculation␈α
of␈α
␈↓αlic[x]␈↓␈α
and␈α
two␈α
table␈αlook-ups␈α
(for␈α
the␈α
value␈α
of␈α
␈↓αy␈↓),␈α
rather␈α
than␈α
the␈αtwo
␈↓ ↓H␈↓calculations␈α
of␈α
␈↓αlic[x]␈↓␈α
in␈α
␈↓αg␈↓.␈α
 More␈αconventional␈α
programming␈α
languages␈α
can␈α
obtain␈α
the␈α
same␈αeffect␈α
as
␈↓ ↓H␈↓this␈α∂use␈α∂of␈α∞internal␈α∂lambdas␈α∂by␈α∞assignment␈α∂of␈α∂␈↓αlic[x]␈↓␈α∞to␈α∂a␈α∂temporary␈α∞variable.␈α∂We␈α∂will␈α∞introduce
␈↓ ↓H␈↓assignment statements in LISP in Section 4.13.






␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 53␈↓␈αOur␈αcurrent␈αLISP␈αsubset␈αhas␈αno␈αside␈αeffects.␈αThat␈αmeans␈αthere␈αis␈αno␈αway␈αfor␈αa␈αcomputation␈αto
␈↓ ↓H␈↓affect␈α⊂its␈α⊂surrounding␈α⊂environment.␈α⊂The␈α⊂most␈α⊂common␈α⊂construct␈α⊂which␈α⊂has␈α⊂a␈α⊂side-effect␈α⊃is␈α⊂the
␈↓ ↓H␈↓assignment statement.
␈↓ ↓H␈↓␈↓↓4.4␈↓ 	}␈↓αλ␈↓↓-notation     99␈↓α


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.  What is the difference between ␈↓αλ[[ ] x*y + y]␈↓ and ␈↓αx*y + y␈↓?



␈↓ ↓H␈↓␈↓ ∧s␈↓↓4.5  Mechanization of evaluation␈↓


␈↓ ↓H␈↓We␈αnow␈αhave␈α
picked␈αa␈αrepresentation␈αfor␈α
LISP␈αexpressions;␈αhave␈α
introduced␈αa␈αprecise␈αnotation␈α
for
␈↓ ↓H␈↓discussing␈α
functions;␈α
and␈α
we␈α
have␈α
given␈α
plausibility␈α
arguments␈α
for␈α
the␈α
existence␈α
of␈α
an␈αevaluator␈α
for
␈↓ ↓H␈↓LISP.␈αIt␈αis␈αnow␈αtime␈αto␈αwrite␈αa␈αformal,␈αprecise,␈αevaluator␈αfor␈αLISP␈αexpressions.␈α The␈αevaluator␈αwill
␈↓ ↓H␈↓be␈α
the␈α
final␈αarbiter␈α
on␈α
the␈α
question␈αof␈α
the␈α
meaning␈αof␈α
a␈α
LISP␈α
construct.␈αThe␈α
evaluator␈α
is␈α
thus␈αa
␈↓ ↓H␈↓very␈α
important␈α
algorithm.␈α
We␈αwill␈α
express␈α
it␈α
and␈α
its␈αrelated␈α
functions␈α
in␈α
as␈αrepresentation-free␈α
form
␈↓ ↓H␈↓as possible, but we will always have our Cambridge polish representation in the back of our minds.

␈↓ ↓H␈↓As␈αwe␈α
have␈αsaid,␈α␈↓αtgmoaf␈↓␈α
and␈α␈↓αtgmoafr␈↓␈α
(Section 3.7)␈αare␈αevaluators␈α
for␈αsubsets␈α
of␈αLISP.␈α Armed␈α
with
␈↓ ↓H␈↓our␈α
symbol-table␈αmechanism␈α
we␈αcould␈α
now␈α
extend␈αthe␈α
␈↓↓great-mothers␈↓␈αto␈α
handle␈α
variable␈αlook-ups.
␈↓ ↓H␈↓Rather␈α∞than␈α∞do␈α∞this␈α∞we␈α∞will␈α∞display␈α∞our␈α∞burgeoning␈α∞cleverness␈α∞and␈α∞make␈α∞a␈α∞total␈α∞revision␈α∞of␈α
the
␈↓ ↓H␈↓structure of the evaluators. In making the revision, the following points should be remembered:

␈↓ ↓H␈↓1.␈α Expressions␈αto␈αbe␈αevaluated␈αcan␈αcontain␈αvariables,␈αboth␈αsimple␈αvariables␈αand␈αvariables␈αnaming
␈↓ ↓H␈↓λ-expressions.␈αThus␈αevaluation␈αmust␈α
be␈αdone␈αwith␈αrespect␈αto␈α
an␈αenvironment␈αor␈αsymbol␈α
table.␈αWe
␈↓ ↓H␈↓wish␈α
to␈α
recognize␈α
other␈α
(translations␈α
of)␈αfunction␈α
names␈α
besides␈α
␈↓αCAR,␈α
CDR,␈α
CONS,␈α
EQ,␈↓␈αand␈↓α␈α
ATOM␈↓
␈↓ ↓H␈↓in␈α∪our␈α∀evaluator,␈α∪but␈α∪explicitly␈α∀adding␈α∪new␈α∪definitions␈α∀to␈α∪the␈α∪evaluator␈α∀in␈α∪the␈α∪style␈α∀of␈α∪the
␈↓ ↓H␈↓recognizers␈α⊂for␈α⊃the␈α⊂five␈α⊂primitives␈α⊃is␈α⊂a␈α⊂bad␈α⊃idea.␈α⊂ Our␈α⊂symbol␈α⊃table␈α⊂should␈α⊂hold␈α⊃the␈α⊂function
␈↓ ↓H␈↓definitions␈α∩and␈α∩the␈α∩evaluator␈α∩should␈α∪contain␈α∩the␈α∩general␈α∩schemes␈α∩for␈α∩finding␈α∪the␈α∩definitions,
␈↓ ↓H␈↓binding␈α
variables␈αto␈α
values,␈α
and␈αevaluating␈α
the␈α
function␈αbody.␈α
 By␈α
now␈αyou␈α
should␈α
be␈αconvinced
␈↓ ↓H␈↓that this process is a reasonably well defined task and could be written in LISP.

␈↓ ↓H␈↓2.␈α
 All␈α
␈↓↓function␈↓␈α
calls␈α
are␈α
to␈α
be␈αevaluated␈α
by-value.␈α
However,␈α
there␈α
are␈α
some␈α
␈↓↓special␈α
forms␈↓␈αwe␈α
have
␈↓ ↓H␈↓seen␈αwhich␈αare␈αnot␈αevaluated␈αin␈αthe␈αnormal␈αmanner.␈α In␈αparticular,␈αconditional␈αexpressions,␈αquoted
␈↓ ↓H␈↓expressions, and lambda expressions are handled differently.

␈↓ ↓H␈↓The␈α∂primary␈α∂function␈α⊂is␈α∂named␈α∂␈↓αeval␈↓␈α∂rather␈α⊂than␈α∂␈↓αsogm␈↓␈α∂(Son␈α∂of␈α⊂Great␈α∂Mother).␈α∂It␈α∂will␈α⊂take␈α∂two
␈↓ ↓H␈↓arguments;␈α⊂the␈α⊃first␈α⊂is␈α⊂a␈α⊃representation␈α⊂of␈α⊃an␈α⊂expression␈α⊂to␈α⊃be␈α⊂evaluated,␈α⊂and␈α⊃the␈α⊂second␈α⊃is␈α⊂a
␈↓ ↓H␈↓representation␈α∞of␈α∞a␈α∞symbol␈α∞table.␈α∞The␈α∞function␈α∞␈↓αeval␈↓␈α∞will␈α∞recognize␈α∞numbers,␈α∞and␈α∞the␈α∂constants␈α∞␈↓αT␈↓
␈↓ ↓H␈↓and␈α
␈↓αNIL␈↓,␈α
and␈α∞if␈α
presented␈α
with␈α
a␈α∞variable,␈α
will␈α
attempt␈α∞to␈α
find␈α
the␈α
value␈α∞of␈α
the␈α
variable␈α∞in␈α
the
␈↓ ↓H␈↓symbol table using ␈↓αassoc␈↓ (Section 4.3).

␈↓ ↓H␈↓␈↓αeval␈↓␈α
also␈αhandles␈α
the␈αspecial␈α
forms␈α
␈↓αcond␈↓␈αand␈α
␈↓αquote␈↓.␈αIf␈α
␈↓αeval␈↓␈α
sees␈αa␈α
conditional␈αexpression␈α
(represented
␈↓ ↓H␈↓by␈α∂␈↓α(COND␈α⊂...)␈↓␈α∂)␈α∂then␈α⊂the␈α∂body␈α∂of␈α⊂the␈α∂␈↓αCOND␈↓␈α∂is␈α⊂passed␈α∂to␈α∂a␈α⊂subfunction␈α∂named␈α⊂␈↓αevcond␈↓.␈α∂␈↓αevcond␈↓
␈↓ ↓H␈↓embodies␈α∀the␈α∀conditional␈α∀expression␈α∀semantics␈α∀as␈α∀described␈α∀on␈α∀page 23.␈α∃ The␈α∀representation,
␈↓ ↓H␈↓␈↓α(QUOTE ␈↓λα␈↓α)␈↓,␈αsignifies␈αthe␈αoccurrence␈αof␈αa␈αconstant,␈α␈↓λα␈↓,␈αwhich␈αis␈αsimply␈αreturned.␈α As␈αfar␈αas␈αthis␈α␈↓αeval␈↓
␈↓ ↓H␈↓␈↓↓100  Evaluation␈↓ 14.5␈↓


␈↓ ↓H␈↓is␈α∪concerned,␈α∪any␈α∪other␈α∪expression␈α∪is␈α∪a␈α∪call-by-value␈α∪function␈α∪application.␈α∪ The␈α∪argument-list
␈↓ ↓H␈↓evaluation␈αis␈α
handled␈αby␈α
␈↓αevlis␈↓␈αin␈αthe␈α
authorized␈αleft-to-right␈α
ordering.␈αThis␈α
is␈αhandled␈αby␈α
recursion
␈↓ ↓H␈↓on␈α∪the␈α∪sequence␈α∪of␈α∪arguments.␈α∩ In␈α∪this␈α∪case␈α∪we␈α∪␈↓↓apply␈↓␈α∩the␈α∪function␈α∪to␈α∪the␈α∪list␈α∪of␈α∩evaluated
␈↓ ↓H␈↓arguments. This is done by the function ␈↓αapply␈↓.

␈↓ ↓H␈↓With␈α
this␈αshort␈α
introduction␈αwe␈α
will␈αnow␈α
write␈α
a␈αmore␈α
general␈αevaluator␈α
which␈αwill␈α
handle␈αa␈α
larger
␈↓ ↓H␈↓subset of LISP than the ␈↓αtgm␈↓s.  Here's the new ␈↓αeval␈↓:

␈↓ ↓H␈↓αeval <= λ[[exp;environ]
␈↓ ↓H␈↓α␈↓ α([isvar[exp] → lookup[exp;environ];
␈↓ ↓H␈↓α␈↓ α( isconst[exp] → denote[exp];
␈↓ ↓H␈↓α␈↓ α( iscond[exp] → evcond[rest[exp];environ];
␈↓ ↓H␈↓α␈↓ α( isfunc+args[exp] → apply[func[exp];evlis[arglist[exp];environ];environ] ]]

␈↓ ↓H␈↓α␈↓and:␈↓α

␈↓ ↓H␈↓αlookup <=λ[[var;env] value[assoc[var;env]]]

␈↓ ↓H␈↓αdenote <= λ[[exp]
␈↓ ↓H␈↓α␈↓ α([isnumber[exp] → exp;
␈↓ ↓H␈↓α␈↓ α( istruth[exp] → exp;
␈↓ ↓H␈↓α␈↓ α( isfalse[exp] → exp;
␈↓ ↓H␈↓α␈↓ α( isSexpr[exp] → rep[exp] ]]

␈↓ ↓H␈↓α␈↓where:␈↓α


␈↓ ↓H␈↓αrep␈↓␈α∂knows␈α∂how␈α∂to␈α∞extract␈α∂the␈α∂S-expr␈α∂from␈α∞the␈α∂representation.␈α∂In␈α∂our␈α∞scheme␈α∂the␈α∂selector␈α∂␈↓αrep␈↓␈α∞is
␈↓ ↓H␈↓␈↓ α8given by ␈↓αcadr␈↓.

␈↓ ↓H␈↓αevcond <= λ[[e;environ]
␈↓ ↓H␈↓α␈↓ α([eval[ante[first[e]];environ] → eval[conseq[first[e]];environ];
␈↓ ↓H␈↓α␈↓ α( ␈↓
t␈↓α → evcond[rest[e];environ] ]]
␈↓ ↓H␈↓α␈↓and,␈↓α

␈↓ ↓H␈↓αevlis <= λ[[e;environ]
␈↓ ↓H␈↓α␈↓ α([null[e] → ( );
␈↓ ↓H␈↓α␈↓ α( ␈↓
t␈↓α → concat[eval[first[e];environ];evlis[rest[e];environ]] ]]

␈↓ ↓H␈↓The␈α∂selectors,␈α∞constructors␈α∂and␈α∞recognizers␈α∂which␈α∞relate␈α∂this␈α∞abstract␈α∂definition␈α∞to␈α∂our␈α∞particular
␈↓ ↓H␈↓S-expression␈α∂representation␈α∞are␈α∂grouped␈α∞with␈α∂␈↓αapply␈↓␈α∞on␈α∂page 101.␈α∞ The␈α∂subfunctions,␈α∂␈↓αevcond␈↓␈α∞and
␈↓ ↓H␈↓␈↓αevlis␈↓,␈α∪are␈α∪simple.␈α∩ ␈↓αevcond␈α∪␈↓you've␈α∪seen␈α∪before␈α∩in␈α∪␈↓αtgmoafr␈↓␈α∪in␈α∪a␈α∩less␈α∪abstract␈α∪form;␈α∪␈↓αevlis␈↓␈α∩simply
␈↓ ↓H␈↓manufactures␈αa␈αnew␈αlist␈αconsisting␈αof␈αthe␈αresults␈αof␈αevaluating␈α(from␈αleft␈αto␈αright)␈αthe␈αelements␈αof␈α␈↓αe␈↓,
␈↓ ↓H␈↓using␈α
the␈αsymbol␈α
table,␈α␈↓αenviron␈↓,␈α
where␈αnecessary.␈α
 Since␈α␈↓αevcond␈↓␈α
and␈α␈↓αevlis␈↓␈α
are␈αLISP␈α
functions,␈α␈↓↓they␈↓
␈↓ ↓H␈↓are␈α∞subject␈α
to␈α∞the␈α∞left-to-right␈α
evaluation␈α∞rule.␈α∞Thus␈α
␈↓αevlis␈↓␈α∞embodies␈α∞the␈α
left-to-right␈α∞rule.␈α∞ If␈α
␈↓αevlis␈↓
␈↓ ↓H␈↓␈↓↓4.5␈↓ π{Mechanization of evaluation     101␈↓


␈↓ ↓H␈↓were␈α
evaluated␈αunder␈α
a␈α
right-to-left␈αrule␈α
then␈α␈↓αevlis␈↓␈α
would␈α
evaluate␈αexpressions␈α
in␈αright-to-left␈α
order.
␈↓ ↓H␈↓It␈αis␈α
possible␈αto␈αwrite␈α
a␈αversion␈αof␈α
␈↓αevlis␈↓␈αwhich␈αonly␈α
depends␈αon␈αbeing␈α
evaluated␈α␈↓	CBV␈↓,␈α
and␈αwhich
␈↓ ↓H␈↓does embody the left-to-right rule:

␈↓ ↓H␈↓αevlis <= λ[[e;environ]
␈↓ ↓H␈↓α␈↓ α([null[e] → ( );
␈↓ ↓H␈↓α␈↓ α( ␈↓
t␈↓α → λ[[x]concat[x;evlis[rest[e];environ]]]
␈↓ ↓H␈↓α␈↓ α(␈↓ αx[eval[first[e];environ]] ]]

␈↓ ↓H␈↓The␈αfunction␈α␈↓αapply␈↓␈αtakes␈αthree␈αarguments:␈αa␈αrepresentation␈αof␈αa␈αfunction,␈αa␈αrepresentation␈αof␈αa␈αlist
␈↓ ↓H␈↓of␈α⊂evaluated␈α⊂arguments,␈α∂and␈α⊂a␈α⊂representation␈α∂of␈α⊂a␈α⊂symbol␈α∂table.␈α⊂␈↓αapply␈↓␈α⊂explicitly␈α⊂recognizes␈α∂the
␈↓ ↓H␈↓representations␈αof␈αthe␈αfive␈αprimitive␈αfunctions␈α␈↓αCAR,␈αCDR,␈αCONS,␈αEQ,␈α␈↓and␈↓α␈αATOM␈↓.␈αIf␈αthe␈αfunction
␈↓ ↓H␈↓name␈α∩is␈α⊃a␈α∩variable,␈α⊃the␈α∩definition␈α⊃is␈α∩located␈α⊃in␈α∩the␈α⊃symbol␈α∩table␈α⊃by␈α∩␈↓αeval␈↓␈α⊃and␈α∩applied␈α∩to␈α⊃the
␈↓ ↓H␈↓arguments.␈α
 Otherwise␈α
the␈α
function␈α∞must␈α
be␈α
a␈α
λ-expression.␈α∞ This␈α
is␈α
where␈α
things␈α∞get␈α
interesting.
␈↓ ↓H␈↓We␈α
know␈α
we␈α∞must␈α
evaluate␈α
the␈α
body␈α∞of␈α
the␈α
λ-expression␈α
after␈α∞binding␈α
the␈α
formal␈α∞parameters␈α
of
␈↓ ↓H␈↓the␈α
λ-expression␈αto␈α
the␈α
evaluated␈αarguments.␈α
How␈α
do␈αwe␈α
bind?␈α
 We␈αadd␈α
variable-value␈α
pairs␈αto␈α
the
␈↓ ↓H␈↓symbol␈αtable.␈αWe␈αwill␈αdefine␈αa␈αsubfunction,␈α␈↓αmkenv␈↓,␈αto␈αperform␈αthe␈αbinding.␈α Then␈αall␈αthat␈αis␈αleft␈α
to
␈↓ ↓H␈↓do is give the function body and the new symbol table to ␈↓αeval␈↓.  Here is ␈↓αapply␈↓:

␈↓ ↓H␈↓αapply <= λ[[fn;args,environ]
␈↓ ↓H␈↓α␈↓ β([iscar[fn] → car[arg␈↓β1␈↓α[args]];
␈↓ ↓H␈↓α␈↓ β( iscons[fn] → cons[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓ ↓H␈↓α␈↓ β(        ...                ...
␈↓ ↓H␈↓α␈↓ β( isvar[fn] → apply[eval[fn;environ];args;environ];
␈↓ ↓H␈↓α␈↓ β( islambda[fn] → eval[body[fn];mkenv[vars[fn];args;environ]] ]]

␈↓ ↓H␈↓αmkenv <= λ[[vars;vals;environ] pairlis[vars;vals;environ]]

␈↓ ↓H␈↓αpairlis <= λ[[vars;vals;environ]
␈↓ ↓H␈↓α␈↓ β([null[vars] → environ;
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → concat[mkent[first[vars];first[vals]];pairlis[rest[vars];rest[vals];environ]] ]]

␈↓ ↓H␈↓Some␈α∂of␈α∂the␈α∂functions␈α∂and␈α∂predicates␈α∂which␈α∂will␈α∂relate␈α∂these␈α∂abstract␈α∂definitions␈α∂to␈α∂our␈α∞specific
␈↓ ↓H␈↓S-expression representation of LISP constructs are:

␈↓ ↓H␈↓↓␈↓ αλRecognizer␈↓ ¬HSelector␈↓ λλConstructor
␈↓ ↓H␈↓αiscar <= λ[[x]eq[x;CAR]]␈↓ ¬Hfunc <= λ[[x]first[x]]␈↓ λλmkent <= λ[[x;y]cons[x;y]]
␈↓ ↓H␈↓αisSexpr <= λ[[x]eq[first[x];QUOTE]]␈↓ ¬Harglist <= λ[[x]rest[x]]
␈↓ ↓H␈↓αistruth <= λ[[x] eq[x;T]]␈↓ ¬Hbody <= λ[[x]third[x]]
␈↓ ↓H␈↓α␈↓ αλ␈↓ ¬Hvars <= λ[[x]second[x]]


␈↓ ↓H␈↓The␈α∃only␈α∀really␈α∃new␈α∃development␈α∀is␈α∃in␈α∀the␈α∃λ-binding␈α∃process.␈α∀ Another␈α∃application␈α∃of␈α∀the
␈↓ ↓H␈↓left-to-right␈αproperty␈αoccurs␈α
here,␈αwithin␈α␈↓αapply␈↓,␈α
in␈αthe␈αsymbol␈α
table␈αsearch␈αand␈αconstruction␈α
process.
␈↓ ↓H␈↓Notice␈α
that␈α
␈↓αlookup␈↓␈α
uses␈α
␈↓αassoc␈↓␈α
to␈α
look␈α
from␈α
left␈α
to␈α
right␈α
for␈α
the␈α
latest␈α
binding␈α
of␈α
a␈α
variable.␈αThus␈α
the
␈↓ ↓H␈↓␈↓↓102  Evaluation␈↓ 14.5␈↓


␈↓ ↓H␈↓function␈α⊃which␈α∩␈↓↓augments␈↓␈α⊃the␈α∩table␈α⊃must␈α⊃add␈α∩the␈α⊃latest␈α∩binding␈α⊃to␈α⊃the␈α∩␈↓↓front␈↓.␈α⊃When␈α∩do␈α⊃new
␈↓ ↓H␈↓bindings␈αoccur?␈α
They␈αoccur␈α
at␈αλ-binding␈αtime,␈α
and␈αat␈α
that␈αtime␈αthe␈α
function␈α␈↓αmkenv␈↓,␈α
using␈α␈↓αpairlis␈↓,
␈↓ ↓H␈↓will␈α∂build␈α∞an␈α∂augmented␈α∞symbol␈α∂table␈α∞with␈α∂the␈α∞λ-variables␈α∂bound␈α∞to␈α∂their␈α∂evaluated␈α∞arguments.
␈↓ ↓H␈↓The␈α∞functions␈α∞␈↓αlookup␈↓␈α
and␈α∞␈↓αmkenv␈↓␈α∞operate␈α
together.␈α∞We␈α∞will␈α
see␈α∞representations␈α∞of␈α∞these␈α
functions
␈↓ ↓H␈↓other␈α∞than␈α∞␈↓αassoc␈↓␈α∞and␈α∞␈↓αpairlis␈↓.␈α∞The␈α∞actual␈α∞search␈α∞and␈α∞construction␈α∞operations␈α∞will␈α∞change,␈α∞but␈α∞the
␈↓ ↓H␈↓critical␈αrelationship␈αthat␈α␈↓αmkenv␈↓␈αalways␈αbuilds␈αa␈αtable␈αcompatable␈αwith␈αthe␈αsearch␈αstrategy␈αof␈α␈↓αlookup␈↓
␈↓ ↓H␈↓will be maintained. ␈↓↓This is important.␈↓

␈↓ ↓H␈↓To␈αsummarize␈α
then:␈αthe␈αevaluation␈α
of␈αan␈αexpression␈α
␈↓αf[a␈↓β1␈↓α; ... ;a␈↓βn␈↓α]␈↓,␈αwhere␈αthe␈α
a␈↓βi␈↓'s␈αare␈αS-exprs,␈α
is␈αthe
␈↓ ↓H␈↓same␈α⊂as␈α⊂the␈α⊂result␈α⊂of␈α⊃applying␈α⊂␈↓αeval␈↓␈α⊂to␈α⊂the␈α⊂␈↓
R␈↓-translation,␈α⊃␈↓α(␈↓
R␈↓∞(␈↓α f ␈↓∞)␈↓α, ␈↓
R␈↓∞(␈↓α a␈↓β1 ␈↓∞)␈↓α, ... ␈↓
R␈↓∞(␈↓α a␈↓βn ␈↓∞)␈↓α).␈↓␈α⊂This
␈↓ ↓H␈↓behavior␈α∞is␈α∞again␈α∞an␈α∞example␈α∞of␈α∞the␈α∞diagrams␈α∞of␈α∞page 55.␈α∞In␈α∞its␈α∞most␈α∞simple␈α∞terms,␈α∞we␈α∞mapped
␈↓ ↓H␈↓LISP␈α∞evaluation␈α∞onto␈α∞the␈α∞LISP␈α∞␈↓αeval␈↓␈α
function;␈α∞mapped␈α∞LISP␈α∞expressions␈α∞onto␈α∞S-expressions;␈α
and
␈↓ ↓H␈↓executed␈α
␈↓αeval␈↓.␈α
Notice␈α
that␈α∞in␈α
this␈α
case␈α
we␈α∞do␈α
not␈α
reinterpret␈α
the␈α∞output␈α
since␈α
the␈α
structure␈α∞of␈α
the
␈↓ ↓H␈↓representation does this implicitly. We have commented on the efficacy of this already on page 94.

␈↓ ↓H␈↓This␈α∞specification␈α∂of␈α∞the␈α∞semantics␈α∂of␈α∞LISP␈α∂using␈α∞␈↓αeval␈↓␈α∞and␈α∂␈↓αapply␈↓␈α∞is␈α∞one␈α∂of␈α∞the␈α∂most␈α∞interesting
␈↓ ↓H␈↓developments of computer science.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.␈α∞Compare␈α∞our␈α∞version␈α∞of␈α∞␈↓αeval␈↓␈α∞and␈α∞␈↓αapply␈↓␈α∞with␈α∞the␈α∞version␈α∞given␈α∞in␈α∞the␈α∞appendix.␈α∞Though␈α
the
␈↓ ↓H␈↓current␈α∂version␈α∂is␈α∂much␈α∂more␈α∂readable,␈α∂how␈α∂much␈α∂of␈α∂it␈α∂␈↓↓still␈↓␈α∂depends␈α∂on␈α∂the␈α⊂representation␈α∂we
␈↓ ↓H␈↓chose? That is, how abstract is it really?

␈↓ ↓H␈↓II. Complete the specification of the selectors, constructors, and recognizers.



␈↓ ↓H␈↓␈↓ ¬?␈↓↓4.6  Examples of ␈↓αeval␈↓↓␈↓α


␈↓ ↓H␈↓We␈α
will␈α
demonstrate␈α
the␈α∞inner␈α
workings␈α
of␈α
the␈α
evaluation␈α∞algorithm␈α
on␈α
a␈α
couple␈α
of␈α∞samples␈α
and
␈↓ ↓H␈↓will␈αdescribe␈αthe␈αflow␈αof␈αcontrol␈αin␈αthe␈αexecution␈αin␈αa␈αcouple␈αof␈αdifferent␈αways.␈αThe␈αexamples␈αwill
␈↓ ↓H␈↓be␈α∞done␈α
in␈α∞terms␈α
of␈α∞the␈α∞image␈α
of␈α∞the␈α
␈↓
R␈↓-mapping␈α∞rather␈α∞than␈α
being␈α∞done␈α
abstractly.␈α∞We␈α∞do␈α
this
␈↓ ↓H␈↓since␈αthe␈αstructure␈αof␈αan␈αactual␈αLISP␈αevaluator␈αwill␈αuse␈αthis␈αrepresentation␈↓π 54␈↓.␈α It␈αis␈αimportant␈αthat
␈↓ ↓H␈↓you␈α⊃diligently␈α⊃study␈α⊃the␈α∩sequence␈α⊃of␈α⊃events␈α⊃in␈α⊃the␈α∩execution␈α⊃of␈α⊃the␈α⊃evaluator.␈α⊃The␈α∩process␈α⊃is
␈↓ ↓H␈↓detailed and tedious but it must be done once so that you may convince yourself of its correctness.

␈↓ ↓H␈↓Let's evaluate ␈↓αf[2;3]␈↓ where ␈↓αf <= λ[[x;y] x␈↓π2␈↓α + y].␈↓ That is, evaluate:

␈↓ ↓H␈↓␈↓ βu␈↓αeval[ ␈↓
R␈↓∞(␈↓α f[2;3] ␈↓∞)␈↓α; ␈↓
R␈↓∞(␈↓α{ <f, λ[[x;y] +[↑[x;2]; y]]> }␈↓∞)␈↓α]␈↓. 
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 54␈↓ Recall that we will be programming in the ␈↓
R␈↓-image.
␈↓ ↓H␈↓␈↓↓4.6␈↓ 	∩Examples of ␈↓αeval␈↓↓     103␈↓α


␈↓ ↓H␈↓After appropriate translation this is equivalent to evaluating:

␈↓ ↓H␈↓␈↓ β+␈↓αeval[(F 2 3); ((F . (LAMBDA (X Y) (PLUS (EXPT X 2) Y))))]␈↓ 

␈↓ ↓H␈↓␈↓↓Notes:␈↓

␈↓ ↓H␈↓␈↓↓1.␈↓␈α∞␈↓α((F␈α∂.␈α∞(LAMBDA␈α∂(X␈α∞Y)␈α∞...␈α∂)))␈α∞=␈α∂((F␈α∞LAMBDA␈α∞(X␈α∂Y)␈α∞...␈α∂))␈α∞␈↓␈α∞This␈α∂is␈α∞mentioned␈α∂because␈α∞LISP
␈↓ ↓H␈↓implementations will print the latter even if you write the former.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α
Since␈α
the␈α
symbol␈α
table␈α␈↓α((F␈α
...))␈↓␈α
occurs␈α
so␈α
frequently␈α
in␈αthe␈α
following␈α
trace,␈α
we␈α
will␈α
abbreviate␈αit␈α
as
␈↓ ↓H␈↓␈↓αst␈↓.␈α⊃Note␈α∩that␈α⊃we␈α∩have␈α⊃no␈α∩mechanism␈α⊃yet␈α⊃for␈α∩permanently␈α⊃increasing␈α∩the␈α⊃repertoire␈α∩of␈α⊃known
␈↓ ↓H␈↓functions.␈α
 We␈α
must␈α
resort␈α
to␈α
the␈α
subterfuge␈α∞of␈α
initializing␈α
the␈α
symbol␈α
table␈α
to␈α
get␈α
the␈α∞function␈α
␈↓αf␈↓
␈↓ ↓H␈↓defined.

␈↓ ↓H␈↓␈↓↓3.␈↓␈α∂For␈α∂this␈α∂example␈α∂we␈α∂must␈α∂assume␈α∂that␈α∂+␈α∂and␈α∂↑␈α∂(exponentiation)␈α∂are␈α∂known␈α∂functions.␈α∞ Thus
␈↓ ↓H␈↓␈↓αapply␈↓ would have to contain recognizers for ␈↓αPLUS␈↓ and ␈↓αTIMES␈↓:
␈↓ ↓H␈↓α        ... atom[fn] → [isplus[fn] → +[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓ ↓H␈↓α                        isexpt[fn] → ↑[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓ ↓H␈↓α                         ... ]
␈↓ ↓H␈↓α       ...
␈↓ ↓H␈↓␈↓↓104  Evaluation␈↓ 24.6␈↓


␈↓ ↓H␈↓So ␈↓αeval[(F 2 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[func[(F 2 3)]; evlis[arglist[(F 2 3)];st];st]
␈↓ ↓H␈↓α␈↓ αX= apply[F;evlis[(2 3);st];st]
␈↓ ↓H␈↓α␈↓ αX= apply[F;(2 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[eval[F;st];(2 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[(LAMBDA (X Y) (PLUS (EXPT X 2) Y)); (2 3);st]

␈↓ ↓H␈↓α␈↓ αX= eval[body[(LAMBDA (X Y) (PLUS (EXPT X 2) Y))];
␈↓ ↓H␈↓α␈↓ αX        mkenv[vars[(LAMBDA (X Y) (PLUS (EXPT X 2) Y))];(2 3);st]]

␈↓ ↓H␈↓α␈↓ αX= eval[(PLUS (EXPT X 2) Y);pairlis[(X Y);(2 3);st]]
␈↓ ↓H␈↓α␈↓ αX= eval[(PLUS (EXPT X 2) Y);((X . 2)(Y . 3)(F LAMBDA (X Y) ...))]

␈↓ ↓H␈↓α␈↓ αX= apply [PLUS; evlis[((EXPT X 2) Y);((X . 2)(Y . 3)..)];((X . 2)...)]

␈↓ ↓H␈↓α␈↓ αX     ␈↓Let's do a little of:␈↓α  evlis[((EXPT X 2) Y);((X . 2)(Y . 3)...)]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[eval[(EXPT X 2);((X . 2)(Y . 3) ...)];
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H           evlis[(Y);((X . 2) ...)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[apply[EXPT;evlis[(X 2);((X . 2)...)];((X . 2) ...]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H           evlis[(Y); ...]]

␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[apply[EXPT;(2 2);((X . 2) ...];evlis[(Y); ...]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[↑[arg␈↓β1␈↓α[(2 2)];arg␈↓β2␈↓α[(2 2)]];evlis[(Y); ... ]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[↑[2;2];evlis[(Y); ... ]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[4;evlis[(Y);((X . 2)(Y . 3) ....)]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[4;concat[eval[Y;((X .2) ...)]; evlis[( );(( ...))]]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= concat[4;concat[3;( )]]
␈↓ ↓H␈↓α␈↓ αX␈↓ ∧H= (4 3)␈↓

␈↓ ↓H␈↓Now back to apply:
␈↓ ↓H␈↓α␈↓ αX= apply[PLUS;(4 3);((X . 2)(Y . 3) ... )]
␈↓ ↓H␈↓α␈↓ αX= +[4;3]
␈↓ ↓H␈↓α␈↓ αX= 7␈↓

␈↓ ↓H␈↓It␈α
should␈α∞now␈α
be␈α
clear␈α∞that␈α
␈↓αeval␈↓␈α∞and␈α
friends␈α
␈↓↓do␈↓␈α∞perform␈α
as␈α∞you␈α
would␈α
expect.␈α∞ It␈α
perhaps␈α∞is␈α
not
␈↓ ↓H␈↓clear␈αthat␈αa␈αsimpler␈αscheme␈αmight␈αnot␈αdo␈αas␈αwell.␈α In␈αparticular,␈αthe␈αcomplexity␈αof␈αthe␈αsymbol␈αtable
␈↓ ↓H␈↓mechanism␈α∂which␈α∂we␈α∂claimed␈α∂was␈α∂so␈α∂important␈α∂has␈α∂not␈α∂been␈α∂exploited.␈α∂The␈α∂next␈α⊂example␈α∂will
␈↓ ↓H␈↓indeed show that a scheme like ours is necessary to keep track of variable bindings.

␈↓ ↓H␈↓Let's sketch the evaluation of ␈↓αfact[3]␈↓ where:

␈↓ ↓H␈↓␈↓ ∧@␈↓αfact <= λ[[x][x = 0 → 1;␈↓
t␈↓α → *[x;fact[x-1]]]];␈↓ 

␈↓ ↓H␈↓that is, ␈↓αeval[(FACT 3);st] ␈↓where ␈↓αst␈↓ names the initial symbol table:

␈↓ ↓H␈↓␈↓ α
␈↓α((FACT .(LAMBDA (X) (COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X)))))))).␈↓ 
␈↓ ↓H␈↓␈↓↓4.6␈↓ 	∩Examples of ␈↓αeval␈↓↓     105␈↓α


␈↓ ↓H␈↓In␈αthis␈αexample␈α
we␈αwill␈αassume␈αthat␈α
the␈αbinary␈αfunction␈α
*,␈αthe␈αunary␈αpredicate␈α
␈↓αzerop <= λ[[x]x = 0]␈↓
␈↓ ↓H␈↓and␈αunary␈αfunction␈α␈↓α␈αsub1 <= λ[[x] x-1]␈↓␈αare␈α
known␈αand␈αare␈αrecognized␈αin␈αthe␈αevaluator␈α
as␈α␈↓αTIMES,
␈↓ ↓H␈↓αZEROP␈↓ and ␈↓αSUB1␈↓ respectively.

␈↓ ↓H␈↓Then␈↓α eval[(FACT 3);st]
␈↓ ↓H␈↓α␈↓ αX= apply[FACT;evlis[(3);st];st]
␈↓ ↓H␈↓α␈↓ αX= apply[(LAMBDA (X) (COND ...));(3);st]
␈↓ ↓H␈↓α␈↓ αX= eval[(COND ((ZEROP X) 1) (T ( ...)));((X . 3) . st)]
␈↓ ↓H␈↓α␈↓ αX= evcond[(((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X)))));((X . 3) . st)]
␈↓ ↓H␈↓α␈↓(Now, let ␈↓αst1␈↓ be␈↓α ((X . 3) . st) )
␈↓ ↓H␈↓α␈↓ αX= eval[(TIMES X (FACT (SUB1 X))); st1]
␈↓ ↓H␈↓α␈↓ αX= apply[TIMES;evlis[(X (FACT (SUB1 X))); st1];st1]
␈↓ ↓H␈↓α␈↓ αX= apply[TIMES;concat[3;evlis[((FACT (SUB1 X))); st1]];st1]

␈↓ ↓H␈↓α␈↓Now things get a little interesting inside ␈↓αevlis:
␈↓ ↓H␈↓α␈↓ αXevlis[((FACT (SUB1 X)));st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx= concat[eval[(FACT (SUB1 X)); st1];( )]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ and ␈↓α eval[(FACT (SUB1 X));st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[FACT;evlis[((SUB1 X));st1];st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[FACT; (2);st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= apply[(LAMBDA (X) (COND ...));(2);st1]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_= eval[(COND ((ZEROP X) 1) ...));((X . 2) . st1)]
␈↓ ↓H␈↓α␈↓ αX␈↓ βx␈↓ ¬_...

␈↓ ↓H␈↓Notice␈αthat␈αwithin␈αthis␈αlatest␈αcall␈αon␈α␈↓αeval␈↓␈αthe␈αsymbol-table-searching␈αfunction,␈α␈↓αlookup␈↓,␈αwill␈α
find␈αthe
␈↓ ↓H␈↓pair␈α␈↓α(X␈α
.␈α2)␈↓␈α
when␈αlooking␈α
for␈αthe␈α
value␈αof␈α
␈↓αx␈↓.␈αThis␈α
is␈αas␈α
it␈αshould␈α
be.␈α But␈α
notice␈αalso␈α
that␈αthe␈α
older
␈↓ ↓H␈↓binding,␈α
␈↓α(X␈α.␈α
3)␈↓,␈α
is␈αstill␈α
around␈αin␈α
the␈α
symbol␈αtable␈α
␈↓αst1␈↓,␈αand␈α
will␈α
become␈αaccessible␈α
once␈αwe␈α
complete
␈↓ ↓H␈↓this␈αlatest␈αcall␈αon␈α␈↓αeval␈↓.␈α It␈αwill␈αbecome␈αaccessible␈αbecause␈αthis␈αearlier␈αmanifestation␈αof␈αthe␈αtable␈αwas
␈↓ ↓H␈↓saved␈α∩by␈α∩the␈α⊃λ-binding␈α∩process␈α∩as␈α⊃we␈α∩entered␈α∩the␈α∩inner␈α⊃call␈α∩on␈α∩␈↓αeval␈↓;␈α⊃as␈α∩we␈α∩leave␈α∩this␈α⊃inner
␈↓ ↓H␈↓evaluation, the previous incarnation of the table is restored.

␈↓ ↓H␈↓As the computation continues, the current symbol table appears as follows:
␈↓ ↓H␈↓α␈↓ ∧B((FACT LAMBDA (X) (COND ...))) = st,
␈↓ ↓H␈↓α␈↓ ¬Z((X . 3) . st) = st␈↓λ'␈↓α,
␈↓ ↓H␈↓α␈↓ ¬O((X . 2) . st␈↓λ'␈↓α) = st␈↓λ''␈↓α,
␈↓ ↓H␈↓α␈↓ ¬G((X . 1) . st␈↓λ''␈↓α) = st␈↓λ'''␈↓α,
␈↓ ↓H␈↓α␈↓ ¬m((X . 0) . st␈↓λ'''␈↓α).

␈↓ ↓H␈↓Thus␈αeach␈αnew␈αlevel␈αof␈αthe␈α
table␈αbuilds␈αon␈αthe␈αprior␈α␈↓αst␈↓;␈αeach␈α
prior␈α␈↓αst␈↓␈αis␈αsaved␈αin␈αthe␈αfollowing␈α
line
␈↓ ↓H␈↓from ␈↓αapply␈↓ (page 101):
␈↓ ↓H␈↓α␈↓ β\islambda[fn] → eval[body[fn];mkenv[vars[fn];args;environ].

␈↓ ↓H␈↓The␈α
call␈α
on␈α
␈↓αeval␈↓␈α
is␈α
performed␈α
with␈α
the␈αaugmented␈α
table;␈α
when␈α
we␈α
leave␈α
that␈α
inner␈α
␈↓αeval␈↓␈α
we␈αreturn␈α
to
␈↓ ↓H␈↓an environment which contains the prior ␈↓αst␈↓.
␈↓ ↓H␈↓␈↓↓106  Evaluation␈↓ 24.6␈↓


␈↓ ↓H␈↓We␈α
claim␈α
that␈αusing␈α
␈↓αmkenv␈↓␈α
to␈αconcat␈α
the␈α
new␈αbindings␈α
onto␈α
the␈αfront␈α
of␈α
the␈αsymbol␈α
table␈α
as␈αwe␈α
call
␈↓ ↓H␈↓␈↓αeval␈↓␈α
does␈α
the␈αright␈α
thing.␈α
 The␈αtricky␈α
part␈α
is␈αthat␈α
when␈α
we␈αleave␈α
that␈α
particular␈αcall␈α
on␈α
␈↓αeval␈↓,␈αthe␈α
old
␈↓ ↓H␈↓table␈α∂is␈α∂automatically␈α∞restored␈α∂by␈α∂the␈α∞recursion␈α∂mechanism.␈α∂ That␈α∞is,␈α∂if␈α∂␈↓αst␈↓␈α∞is␈α∂the␈α∂current␈α∞symbol
␈↓ ↓H␈↓table␈αthen␈α␈↓αconcat␈↓-ing␈αthings␈αonto␈αthe␈αfront␈αof␈α␈↓αst␈↓␈αdoesn't␈αchange␈α␈↓αst␈↓,␈αbut␈αif␈αwe␈αcall␈α␈↓αeval␈↓␈αor␈α␈↓αapply␈↓␈αwith
␈↓ ↓H␈↓a symbol table of say:

␈↓ ↓H␈↓␈↓ ∧y␈↓αconcat[(X . 2);concat[(X . 3); st]] ␈↓ 

␈↓ ↓H␈↓then in ␈↓↓that␈↓ call on ␈↓αeval␈↓ or ␈↓αapply␈↓ we have access to ␈↓αx = 2␈↓, not ␈↓αx = 3␈↓.

␈↓ ↓H␈↓In␈α∞this␈α∞representation,␈α∞the␈α
search␈α∞function␈α∞␈↓αlookup␈↓␈α∞always␈α∞proceeds␈α
from␈α∞left␈α∞to␈α∞right␈α∞through␈α
the
␈↓ ↓H␈↓table␈αand,␈α
since␈αthe␈αtable␈α
entry␈αfunction␈α␈↓αmkenv␈↓␈α
always␈α␈↓αconcat␈↓s␈αpairs␈α
onto␈αthe␈αleft␈α
of␈αthe␈αtable␈α
before
␈↓ ↓H␈↓␈↓αeval␈↓ is called, we will get the correct binding of the variables.

␈↓ ↓H␈↓The␈αstructure␈α
of␈α␈↓αmkenv␈↓␈α
should␈αbe␈αanalyzed␈α
further:␈αit␈α
takes␈αa␈αformal␈α
parameter␈αlist,␈α
an␈αevaluated
␈↓ ↓H␈↓actual␈αparameter␈αlist,␈αand␈αan␈αenvironment,␈αas␈αits␈αarguments;␈αit␈αallocates␈αa␈αnew␈αblock␈αto␈αcontain␈αthe
␈↓ ↓H␈↓name-value␈α
pairs␈α
and␈α
proceeds␈α
to␈α
send␈α
each␈α
name-value␈α
pair␈α
to␈α
its␈α
proper␈α
slot␈α
in␈α
the␈α
block.␈α
The
␈↓ ↓H␈↓value␈α∞of␈α∞␈↓αmkenv␈↓␈α∞is␈α∞the␈α∞newly␈α∞constructed␈α∞environment␈α∞formed␈α∞by␈α∞linking␈α∞the␈α∞new␈α∞block␈α∞onto␈α∞the
␈↓ ↓H␈↓front␈αof␈αthe␈αold␈αenvironment.␈αIt␈αturns␈αout␈αthat␈α␈↓αpairlis␈↓␈αis␈αable␈αto␈αcombine␈αthe␈αaction␈αof␈αmaking␈αthe
␈↓ ↓H␈↓new block and filling the slots.

␈↓ ↓H␈↓A more accurate picture of the abstract behavior of ␈↓αmkenv␈↓ is:

␈↓ ↓H␈↓αmkenv <= λ[[vars;vals;env] mkenv␈↓λ'␈↓α[vars;vals;alloc[vars];env]]

␈↓ ↓H␈↓αmkenv␈↓λ'␈↓α <= λ[[vars;vals;block;env]␈↓ ¬λ[null[vars] → link[block;env];
␈↓ ↓H␈↓α␈↓ ¬λ ␈↓
t␈↓α → mkenv␈↓λ'␈↓α[␈↓ ε(rest[vars];
␈↓ ↓H␈↓α␈↓ ¬λ␈↓ ε(rest[vals];
␈↓ ↓H␈↓α␈↓ ¬λ␈↓ ε(send[first[vars];first[vals];block];
␈↓ ↓H␈↓α␈↓ ¬λ␈↓ ε(env] ]]]
␈↓ ↓H␈↓Our current implementation of ␈↓αpairlis␈↓ is equivalent to:
␈↓ ↓H␈↓␈↓ ¬P␈↓αalloc <=λ[[x] ( )] ␈↓π 55␈↓α

␈↓ ↓H␈↓α␈↓ βzsend <= λ[[var;val;block] concat[mkent[var;val];block]]

␈↓ ↓H␈↓α␈↓ ∧Qlink <= λ[[block;env] append[block;env]]

␈↓ ↓H␈↓The␈αcomputational␈αbehavior␈αof␈α␈↓αpairlis␈↓␈αis␈αslightly␈αdifferent:␈αthe␈αname␈αvalue␈αpairs␈αare␈αadded␈αto␈αthe
␈↓ ↓H␈↓environment␈αin␈αa␈αdifferent␈αorder.␈αSince␈αthe␈αvariables␈αin␈αthe␈αλ-list␈αare␈αrequired␈αto␈αbe␈α
distinct␈αfrom
␈↓ ↓H␈↓one another, the resulting environment is equivalent.

␈↓ ↓H␈↓Symbol table manipulation is very important, so let's look at it again in a slightly different manner.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓α␈↓π 55␈↓α␈α
alloc␈↓␈αis␈α
defined␈αas␈α
a␈α
unary␈αfunction␈α
even␈αthough␈α
its␈α
argument␈αis␈α
ignored␈αhere.␈α
This␈αgenerality␈α
is
␈↓ ↓H␈↓in anticipation of future binding implementations.
␈↓ ↓H␈↓␈↓↓4.6␈↓ 	∩Examples of ␈↓αeval␈↓↓     107␈↓α


␈↓ ↓H␈↓In␈αthis␈αexample,␈αexpressions␈αand␈αtable␈αentries␈αwill␈αbe␈αwritten␈αmore␈αinformally.␈αSince␈α
the␈αevaluator
␈↓ ↓H␈↓is␈α∩operating␈α∩on␈α∩the␈α∩S-expr␈α∩representation␈α∩of␈α∩expressions␈α∩we␈α∩should␈α∩continue␈α∩to␈α∩present␈α∩these
␈↓ ↓H␈↓arguments␈α↔to␈α↔␈↓αeval␈↓␈α_as␈α↔S-exprs.␈α↔ However,␈α↔the␈α_object␈α↔being␈α↔represented␈α↔is␈α_frequently␈α↔more
␈↓ ↓H␈↓understandable␈α
and␈α∞readable␈α
than␈α∞the␈α
representation␈α∞of␈α
that␈α∞object.␈α
Thus,␈α∞initially,␈α
we␈α∞will␈α
write
␈↓ ↓H␈↓␈↓αquote[␈↓λx␈↓α]␈↓␈↓π 56␈↓␈α⊂rather␈α⊃than␈α⊂the␈α⊂explicit␈α⊃␈↓
R␈↓-image␈α⊂of␈α⊃␈↓λx␈↓;␈α⊂for␈α⊂example,␈α⊃write␈α⊂␈↓αquote[fact[3]]␈↓␈α⊃rather␈α⊂than
␈↓ ↓H␈↓␈↓α(FACT 3)␈↓.␈α∂ Later␈α∂we␈α∂will␈α∂simply␈α∂write␈α∂␈↓λx␈↓,␈α∂without␈α∂the␈α∂␈↓αquote␈↓␈α∂where␈α∂no␈α∂confusion␈α∂is␈α⊂likely.␈α∂ With
␈↓ ↓H␈↓similar␈αmotivation,␈αwe␈αrepresent␈αthe␈αsymbol␈αtable␈αbetween␈αvertical␈αbars,␈α"|",␈αin␈αsuch␈αa␈αway␈αthat␈αif␈αa
␈↓ ↓H␈↓table, t␈↓β1␈↓, is:

␈↓ ↓H␈↓␈↓ αX|  b␈↓βn␈↓␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|   ...␈↓ βH| then ␈↓αconcat␈↓ing a new element, b␈↓βn+1␈↓ onto t␈↓β1␈↓ gives:
␈↓ ↓H␈↓␈↓ αX|  b␈↓β1␈↓␈↓ βH|


␈↓ ↓H␈↓␈↓ αX|  b␈↓βn+1␈↓␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|  b␈↓βn␈↓␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|   ...␈↓ βH|
␈↓ ↓H␈↓␈↓ αX|  b␈↓β1␈↓␈↓ βH|


␈↓ ↓H␈↓Note␈α∂that␈α⊂the␈α∂elements␈α∂of␈α⊂the␈α∂table␈α⊂should␈α∂also␈α∂be␈α⊂presented␈α∂as␈α∂S-exprs.␈α⊂We␈α∂will␈α⊂represent␈α∂the
␈↓ ↓H␈↓entires in a more transparent form as simple equations. Thus, for example:

␈↓ ↓H␈↓αeval[quote[fact[3]]; | fact = λ[[x][x=0 → 1;␈↓
t␈↓α → *[x;fact[x-1]]]] | ]

␈↓ ↓H␈↓α␈↓ αX= eval[quote[[x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]];␈↓ λ8|x = 3         |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|fact = λ[[ ...  |   ]

␈↓ ↓H␈↓α␈↓ αX= *[3;eval[quote[[x=0 → ...];␈↓ λ8|    x = 2      |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|    x = 3      |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|fact = λ[[ ...  |   ]


␈↓ ↓H␈↓α␈↓ αX= *[3; *[2;eval[quote[[x=0 → ...];␈↓ λ8|    x = 1     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|    x = 2     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|    x = 3     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|fact = λ[[ ... |   ]







␈↓ ↓H␈↓________________
␈↓ ↓H␈↓α␈↓π 56␈↓α ␈↓See page 92 for ␈↓αquote␈↓.
␈↓ ↓H␈↓␈↓↓108  Evaluation␈↓ 24.6␈↓


␈↓ ↓H␈↓α␈↓ αX= *[3; *[2; *[1;eval[quote[[x=0 → ...];␈↓ λ8|    x = 0     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|    x = 1     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|    x = 2     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|    x = 3     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|fact = λ[[ ... |   ]

␈↓ ↓H␈↓α␈↓ αX= *[3; *[2; *[1;1]]]  ␈↓with:␈↓α␈↓ λ8|     x = 1     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|     x = 2    |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|      ...         |

␈↓ ↓H␈↓α␈↓ αX= *[3; *[2;1]]         ␈↓with:␈↓α␈↓ λ8|     x = 2     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|      ...         |

␈↓ ↓H␈↓α␈↓ αX= *[3;2]               ␈↓with:␈↓α␈↓ λ8|     x = 3     |
␈↓ ↓H␈↓α␈↓ αX␈↓ λ8|     ...          |

␈↓ ↓H␈↓α␈↓ αX= 6.                   ␈↓with:␈↓α␈↓ λ8|fact = λ[[ ... |.

␈↓ ↓H␈↓α= 6

␈↓ ↓H␈↓Notice␈αthat␈αafter␈αwe␈αwent␈αto␈αall␈αthe␈αtrouble␈αto␈αsave␈αthe␈αold␈αvalues␈αof␈α␈↓αx␈↓␈αwe␈αnever␈αhad␈αto␈αuse␈αthem.
␈↓ ↓H␈↓However,␈α
in␈αthe␈α
general␈α
case␈αof␈α
recursive␈α
evaluation␈αwe␈α
must␈α
be␈αable␈α
to␈α
save␈αand␈α
restore␈α
the␈αold
␈↓ ↓H␈↓values of variables.

␈↓ ↓H␈↓For example, recall the definition of ␈↓αequal:


␈↓ ↓H␈↓αequal <= λ[[x;y]
␈↓ ↓H␈↓α␈↓ βH[atom[x] → [atom[y] → eq[x;y]; ␈↓
t␈↓α → ␈↓
f␈↓α];
␈↓ ↓H␈↓α␈↓ βH atom[y] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βH equal[car[x];car[y]] → equal[cdr[x];cdr[y]];
␈↓ ↓H␈↓α␈↓ βH ␈↓
t␈↓α → ␈↓
f␈↓α]]

␈↓ ↓H␈↓α␈↓If we were evaluating:␈↓α
␈↓ ↓H␈↓α␈↓ ¬equal[((A . B) . C);((A . B) . D)],
␈↓ ↓H␈↓then our symbol table structure would  change as follows:

␈↓ ↓H␈↓α␈↓ αX|equal = λ[[x;y] ...| ==>␈↓ ε8|x = ((A . B) . C)| ==>
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8|y = ((A . B) . D)|
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8|equal = λ[[x;y]...|
␈↓ ↓H␈↓␈↓↓4.6␈↓ 	∩Examples of ␈↓αeval␈↓↓     109␈↓α



␈↓ ↓H␈↓α␈↓ αX|   x = (A . B)     |␈↓ ε8|       x = A        |
␈↓ ↓H␈↓α␈↓ αX|   y = (A . B)     |␈↓ ε8|       y = A        |
␈↓ ↓H␈↓α␈↓ αX| x = ((A . B). C) |  ==>␈↓ ε8|    x = (A . B)    |
␈↓ ↓H␈↓α␈↓ αX| y = ((A . B). D) |␈↓ ε8|    y = (A . B)    | ==>
␈↓ ↓H␈↓α␈↓ αX|equal = λ[[x;y ... |␈↓ ε8| x = ((A . B). C) |
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8| y = ((A . B). D) |
␈↓ ↓H␈↓α␈↓ αX␈↓ ε8|equal = λ[[x;y] ...|

␈↓ ↓H␈↓α␈↓ αX|       x = B         |␈↓ ε8|        x = C        |
␈↓ ↓H␈↓α␈↓ αX|       y = B         |␈↓ ε8|        y = D        |
␈↓ ↓H␈↓α␈↓ αX|    x = (A . B)     |␈↓ ε8|  x = ((A . B). C)  | ==>
␈↓ ↓H␈↓α␈↓ αX|    y = (A . B)     | ==>␈↓ ε8|  y = ((A . B). D)  |
␈↓ ↓H␈↓α␈↓ αX| x = ((A . B). C)  |␈↓ ε8|equal = λ[[x;y] ...  |
␈↓ ↓H␈↓α␈↓ αX| y = ((A . B). D)  |
␈↓ ↓H␈↓α␈↓ αX|equal = λ[[x;y] ... |


␈↓ ↓H␈↓α␈↓ ¬X|equal = λ[[x;y] ... |

␈↓ ↓H␈↓This␈α
complexity␈α
is␈α
necessary,␈αfor␈α
while␈α
we␈α
are␈α
evaluating␈α␈↓αequal[car[x];car[y]]␈↓,␈α
we␈α
rebind␈α
␈↓αx␈↓␈α
and␈α␈↓αy␈↓␈α
but
␈↓ ↓H␈↓we must save the old values of ␈↓αx␈↓ and ␈↓αy␈↓ for the possible evaluation of ␈↓αequal[cdr[x];cdr[y]].␈↓

␈↓ ↓H␈↓Before␈αmoving␈αon␈αwe␈αshould␈αexamine␈α␈↓αeval␈↓␈αand␈α␈↓αapply␈↓␈αto␈αsee␈αhow␈αthey␈αcompare␈αwith␈αour␈αprevious
␈↓ ↓H␈↓discussions␈α
of␈α
LISP␈α
evaluation.␈α
 The␈α
spirit␈α
of␈α
call-by-value␈α
and␈α
conditional␈α
expression␈α
evaluation␈α
is
␈↓ ↓H␈↓maintained.␈α
 λ-binding␈α
seems␈α
correct,␈α
though␈α
our␈αcurrent␈α
discussion␈α
is␈α
not␈α
complete.␈α
 At␈α
least␈αone
␈↓ ↓H␈↓preconception␈αis␈αnot␈αmaintained␈αhere.␈αRecall␈αthe␈αdiscussion␈αon␈αpage 21.␈αWe␈αwanted␈αn-ary␈αfunctions
␈↓ ↓H␈↓called␈αwith␈αexactly␈αn␈α
arguments.␈αAn␈αexamination␈αof␈α
the␈αstructure␈αof␈α␈↓αeval␈↓␈α
and␈α␈↓αapply␈↓␈αshows␈αthat␈αif␈α
a
␈↓ ↓H␈↓function␈α∞expecting␈α
␈↓↓n␈↓␈α∞arguments␈α
is␈α∞presented␈α
with␈α∞fewer,␈α
then␈α∞the␈α
result␈α∞is␈α
undefined;␈α∞but␈α
if␈α∞it␈α
is
␈↓ ↓H␈↓given ␈↓↓more␈↓ arguments than necessary then the calculation is performed. For example:

␈↓ ↓H␈↓αeval[(CONS(QUOTE A)(QUOTE B)(QUOTE C));NIL]
␈↓ ↓H␈↓α␈↓ ¬H= eval[(CONS(QUOTE A)(QUOTE B));NIL]
␈↓ ↓H␈↓α␈↓ ¬H= (A . B)

␈↓ ↓H␈↓This␈α∞shows␈α∂one␈α∞of␈α∂the␈α∞pitfalls␈α∂in␈α∞defining␈α∂a␈α∞language␈α∂by␈α∞an␈α∂evaluator.␈α∞ If␈α∂the␈α∞intuitions␈α∂of␈α∞the
␈↓ ↓H␈↓language␈αspecifiers␈α
are␈αfaulty␈α
or␈αincomplete␈α
then␈αeither␈αwe␈α
are␈αfaced␈α
with␈αmaintaining␈α
that␈αfaulty
␈↓ ↓H␈↓judgement, or we must lobby for a "revised report".

␈↓ ↓H␈↓The␈αdefinition␈α
of␈αa␈α
language␈αby␈α
an␈αevaluator␈αwritten␈α
in␈αthat␈α
language␈αis␈α
subject␈αto␈αother␈α
criticisms.
␈↓ ↓H␈↓The␈αtroublesome␈αareas␈αof␈αour␈αdescription␈αof␈αLISP's␈αevaluation␈αincluded␈αλ-binding,␈αcalling␈αstyles␈αin
␈↓ ↓H␈↓general␈α⊃and␈α⊂call-by-value␈α⊃in␈α⊂particular,␈α⊃and␈α⊂left-to-right␈α⊃order␈α⊂of␈α⊃evaluation.␈α⊂We␈α⊃wrote␈α⊃␈↓αeval␈↓␈α⊂to
␈↓ ↓H␈↓explicate␈αthe␈αmeaning␈αof␈αthese␈αconstructs,␈αyet␈α
within␈α␈↓αeval␈↓␈αwe␈αoften␈αrelied␈αon␈αexactly␈αthese␈α
constructs
␈↓ ↓H␈↓to␈α
convey␈α
our␈α
intent.␈α
Now,␈α
our␈α
description␈α
in␈α
not␈α
entirely␈α
circular;␈α
␈↓αeval␈↓␈α
does␈α
convey␈α
much␈α∞of␈α
our
␈↓ ↓H␈↓intention␈α
to␈α
the␈αreader.␈α
However␈α
much␈αof␈α
the␈α
discussion␈αof␈α
␈↓↓how␈↓␈α
these␈αconstructs␈α
operate␈α
is␈αeither
␈↓ ↓H␈↓␈↓↓110  Evaluation␈↓ 24.6␈↓


␈↓ ↓H␈↓implicit␈αor␈αis␈αexplained␈αby␈αusing␈αthe␈α
same␈αconstructs.␈α In␈αgaining␈αa␈αclearer␈αunderstanding␈α
of␈αwhat
␈↓ ↓H␈↓LISP␈αconstructs␈αmean,␈α␈↓αeval␈↓␈αis␈αexemplary.␈αIndeed␈αmany␈αof␈αthe␈αdetails␈αof␈αhow␈αthese␈αconstructs␈αwork
␈↓ ↓H␈↓are␈α⊃irrelevant␈α⊂to␈α⊃such␈α⊃an␈α⊂understanding.␈α⊃However,␈α⊂when␈α⊃we␈α⊃attempt␈α⊂to␈α⊃implement␈α⊃a␈α⊂language
␈↓ ↓H␈↓feature␈αwe␈αcannot␈αassume␈αthe␈αexistence␈αof␈αthat␈αfeature;␈αthe␈αimplementation␈αmust␈αbe␈αprepared␈αfrom
␈↓ ↓H␈↓a␈α
combination␈αof␈α
more␈α
primitive␈αcomponents.␈α
As␈α
we␈αprocede␈α
through␈α
the␈αtext␈α
we␈α
will␈αmake␈α
explicit
␈↓ ↓H␈↓the␈α∩mechanisms␈α∩which␈α∩are␈α∩necessary␈α∩to␈α∩correctly␈α∩implement␈α∩LISP's␈α∩constructs␈α∩and␈α∩indeed␈α∩the
␈↓ ↓H␈↓constucts␈α
of␈α∞most␈α
other␈α
languages.␈α∞In␈α
Section 4.15␈α∞we␈α
give␈α
several␈α∞alternative␈α
algorithms␈α∞for␈α
␈↓αeval␈↓.
␈↓ ↓H␈↓They␈α
will␈α
evolve␈α
to␈αan␈α
␈↓αeval␈↓␈α
which␈α
makes␈αexplicit␈α
most␈α
of␈α
the␈αmechanisms␈α
we␈α
need.␈α
In␈αChapter 5
␈↓ ↓H␈↓we␈αwill␈α
begin␈αto␈α
discuss␈αefficient␈α
representations␈αfor␈α
LISP's␈αdata␈α
structures,␈αcontrol␈α
structures,␈αand
␈↓ ↓H␈↓primitive operations.

␈↓ ↓H␈↓The␈α⊂remainder␈α⊃of␈α⊂this␈α⊂chapter␈α⊃will␈α⊂explicate␈α⊂further␈α⊃features␈α⊂of␈α⊂LISP␈α⊃in␈α⊂preparation␈α⊃for␈α⊂that
␈↓ ↓H␈↓discussion.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓␈↓↓I␈↓␈α∀Which␈α∀parts␈α∀of␈α∃␈↓αeval␈↓␈α∀and␈α∀Co.␈α∀allow␈α∀correct␈α∃evaluation␈α∀of␈α∀functions␈α∀applied␈α∀to␈α∃too␈α∀many
␈↓ ↓H␈↓arguments?

␈↓ ↓H␈↓␈↓↓II␈↓␈α∩On␈α⊃page 109␈α∩we␈α⊃noted␈α∩that␈α⊃the␈α∩evaluator␈α⊃performs␈α∩"correctly"␈α⊃when␈α∩evaluating␈α∩forms␈α⊃like
␈↓ ↓H␈↓␈↓αcons[A;B;C]␈↓.␈α Can␈αyou␈αfind␈αother␈αanomalies␈αin␈α␈↓αeval␈↓␈αand␈αfriends?␈αThat␈αis,␈αplaces␈αwhere␈αunexpected
␈↓ ↓H␈↓results are obtained?



␈↓ ↓H␈↓␈↓ ¬t␈↓↓4.7  Variables␈↓


␈↓ ↓H␈↓Let's␈αlook␈αmore␈αclosely␈αat␈αλ-binding␈αin␈α␈↓αeval␈↓.␈αThe␈αscheme␈αpresented␈αseems␈αreasonable,␈αbut␈αas␈αin␈αthe
␈↓ ↓H␈↓case ␈↓αcons[A;B;C]␈↓, there may be more going on here than we are perhaps aware of.

␈↓ ↓H␈↓If␈αwe␈αasked␈α␈↓αeval␈↓␈α
to␈αcompute␈α␈↓αf[2]␈↓,␈αgiven␈αa␈α
representation␈αfor␈α␈↓αf <= λ[[x] x + y]␈↓␈αbut␈α
no␈αrepresenation
␈↓ ↓H␈↓for␈α∞the␈α∞value␈α∞of␈α∞␈↓αy␈↓␈α∞it␈α∞would␈α∞complain.␈α∞It␈α∞would␈α∞it␈α∞would␈α∞find␈α∞␈↓αf␈↓,␈α∞bind␈α∞␈↓α2␈↓␈α∞to␈α∞␈↓αx␈↓;␈α∞it␈α∞would␈α∂begin␈α∞the
␈↓ ↓H␈↓evaluation␈αof␈αthe␈αbody␈αof␈α␈↓αf␈↓,␈αfinding␈α␈↓αx␈↓'s␈αvalue,␈αbut␈αthen␈αwould␈αfind␈αno␈αvalue␈αfor␈α␈↓αy␈↓.␈αHowever,␈αif␈αwe
␈↓ ↓H␈↓asked␈αit␈αto␈αevaluate␈αthe␈αform␈α␈↓αλ[[y]f[2]][1]␈↓␈αit␈α␈↓↓would␈↓␈αwork.␈αIt␈αwould␈αfind␈αthe␈αvalue␈αof␈α␈↓αy␈↓␈αto␈αbe␈α␈↓α1␈↓␈αand
␈↓ ↓H␈↓would get a final answer of ␈↓α3␈↓ as expected. You should convince yourself of this assertion.

␈↓ ↓H␈↓Within␈αthe␈αevaluation␈αof␈α␈↓αf[2]␈↓␈αin␈α␈↓αλ[[y]f[2]][1]␈↓␈αthe␈αvariable␈α␈↓αy␈↓␈αhas␈αa␈αdifferent␈αcharacter␈αfrom␈αthat␈αof
␈↓ ↓H␈↓␈↓αx␈↓.␈α∞ The␈α∞value␈α∂of␈α∞␈↓αx␈↓␈α∞is␈α∂found␈α∞within␈α∞the␈α∞latest␈α∂λ-binding,␈α∞whereas␈α∞␈↓αy␈↓␈α∂was␈α∞bound␈α∞in␈α∂a␈α∞dynamically
␈↓ ↓H␈↓surrounding␈α
λ-binding.␈α
That␈α
is,␈αthe␈α
λ-expression␈α
which␈α
bound␈α
␈↓αy␈↓␈αtook␈α
effect␈α
before␈α
the␈α
binding␈αof␈α
␈↓αx␈↓
␈↓ ↓H␈↓and␈αis␈αstill␈αin␈αeffect␈αwhen␈αthe␈αbinding␈αof␈α␈↓αx␈↓␈αis␈αmade.␈αWe␈αdo␈αhave␈αaccess␈αto␈α␈↓αy␈↓'s␈αbinding␈αin␈αthis␈αcase;
␈↓ ↓H␈↓the␈α␈↓αlookup␈↓␈αroutine␈αwill␈αlocate␈α␈↓αy␈↓'s␈αvalue.␈α There␈αis␈αa␈αthird␈αkind␈αof␈αname-value␈αassociation␈αpresent␈αin
␈↓ ↓H␈↓␈↓↓4.7␈↓ 	{Variables     111␈↓


␈↓ ↓H␈↓these␈α∞examples:␈α∞we␈α∞expect␈α∞that␈α
the␈α∞symbol␈α∞"+"␈α∞is␈α∞recognized␈α
during␈α∞the␈α∞evaluation␈α∞as␈α∞denoting␈α
a
␈↓ ↓H␈↓procedure␈αfor␈αcomputing␈αthe␈αsum␈αof␈α
two␈αnumbers.␈α In␈αprevious␈αdiscussions␈αwe␈αhave␈α
assumed␈αthat
␈↓ ↓H␈↓"+"␈αwas␈αpre-defined␈αinside␈α␈↓αapply␈↓␈αand␈αtherefore␈αexplicitly␈αrecognized.␈αFinally,␈αin␈αthe␈αfirst␈αexample,␈α
a
␈↓ ↓H␈↓fourth␈α∀kind␈α∪of␈α∀variable␈α∀usage␈α∪occurred.␈α∀The␈α∀variable␈α∪␈↓αy␈↓␈α∀had␈α∀no␈α∪associated␈α∀value␈α∀when␈α∪the
␈↓ ↓H␈↓computation expected one. In this section we wish to examine properties of variables.

␈↓ ↓H␈↓The␈αimplementation␈αof␈αλ-bindings␈αdescribed␈αin␈α␈↓αpairlis␈↓␈α(page 100)␈αis␈αslightly␈αmisleading.␈αThere,␈αthe
␈↓ ↓H␈↓new␈α∞λ-bindings␈α∞are␈α∞␈↓αconcat␈↓-ed␈α∞onto␈α∞the␈α∞front␈α∞of␈α
the␈α∞existing␈α∞table.␈α∞They␈α∞go␈α∞on␈α∞in␈α∞a␈α
one-at-a-time
␈↓ ↓H␈↓fashion␈α
even␈αthough␈α
they␈α
are␈αto␈α
be␈α
thought␈αof␈α
as␈α
a␈αlogical␈α
unit:␈α
at␈αthe␈α
language␈α
level␈αthey␈α
all␈αgo␈α
on
␈↓ ↓H␈↓together,␈α⊂and␈α⊂they␈α∂all␈α⊂come␈α⊂off␈α∂together.␈α⊂It␈α⊂is␈α⊂the␈α∂structure␈α⊂of␈α⊂this␈α∂table␈α⊂which␈α⊂we␈α⊂should␈α∂also
␈↓ ↓H␈↓examine. To these ends we now introduce some terminology.

␈↓ ↓H␈↓Consider the evaluation of the expression:

␈↓ ↓H␈↓α␈↓ ∧Rλ[[y]equal[λ[[x]cons[x;y]][(A . B)];x]][A]

␈↓ ↓H␈↓in an environment where the definition of ␈↓αequal␈↓ is known.

␈↓ ↓H␈↓We␈α⊃evaluate␈α⊃the␈α⊃main␈α⊃argument␈α∩␈↓αA␈↓,␈α⊃and␈α⊃perform␈α⊃the␈α⊃λ-binding␈α∩of␈α⊃␈↓αA␈↓␈α⊃to␈α⊃␈↓αy␈↓.␈α⊃This␈α∩operation␈α⊃of
␈↓ ↓H␈↓λ-binding␈αcreates␈α
what␈αwe␈α
call␈αa␈α␈↓↓local␈α
symbol␈αtable␈↓␈α
and␈αthe␈αvariables␈α
bound␈αin␈α
that␈αlocal␈αtable␈α
are
␈↓ ↓H␈↓called␈α⊃␈↓↓local␈α∩bindings␈↓␈α⊃for␈α∩the␈α⊃body␈α⊃of␈α∩the␈α⊃λ-expression.␈α∩We␈α⊃now␈α⊃begin␈α∩the␈α⊃evaluation␈α∩of␈α⊃the
␈↓ ↓H␈↓arguments␈α
to␈α
␈↓αequal␈↓.␈α
The␈α
first␈α
argument␈αis␈α
itself␈α
an␈α
expression␈α
requiring␈α
λ-binding.␈α
 We␈αevaluate␈α
it's
␈↓ ↓H␈↓argument␈α
and␈αbind␈α
␈↓α(A . B)␈↓␈αto␈α
␈↓αx␈↓.␈αThis␈α
creates␈αa␈α
local␈αbinding␈α
for␈α␈↓αx␈↓.␈α
In␈αthe␈α
process␈αof␈α
making␈α␈↓αx␈↓␈α
local
␈↓ ↓H␈↓what␈αhappens␈αto␈α
␈↓αy␈↓?␈α Notice␈αthat␈α
the␈αbinding␈αprocess␈α
has␈αnot␈αmade␈α
␈↓αy␈↓␈αinaccessible:␈αwe␈α
can␈αcompute
␈↓ ↓H␈↓␈↓αcons[x;y]␈↓␈α∞even␈α∞though␈α∂␈↓αy␈↓␈α∞is␈α∞not␈α∂local.␈α∞Variables␈α∞like␈α∞␈↓αy␈↓␈α∂which␈α∞are␈α∞accessible,␈α∂but␈α∞not␈α∞local,␈α∂we␈α∞call
␈↓ ↓H␈↓␈↓↓non-local␈↓␈α
variables.␈α
 Thus␈α
both␈α
␈↓αy␈↓␈α
and␈α
␈↓αcons␈↓␈αare␈α
non-local␈α
variables␈α
in␈α
our␈α
evaluation␈α
of␈α␈↓αcons[x;y]␈↓.
␈↓ ↓H␈↓There␈α∂is␈α∂a␈α∞further␈α∂distinction␈α∂between␈α∞␈↓αy␈↓␈α∂and␈α∂␈↓αcons␈↓:␈α∞We␈α∂expect␈α∂␈↓αcons␈↓␈α∞to␈α∂be␈α∂a␈α∂predefined␈α∞function;
␈↓ ↓H␈↓indeed␈α
␈↓αcons␈↓␈αhas␈α
not␈αbeen␈α
λ-bound␈αany␈α
where␈α
in␈αour␈α
computation.␈αVariables␈α
like␈α␈↓αcons␈↓␈α
we␈α
will␈αcall
␈↓ ↓H␈↓␈↓↓global variables␈↓.

␈↓ ↓H␈↓Global␈α
variables␈α
include␈α
predefined␈α
function␈αnames,␈α
␈↓αcar␈↓,␈α
␈↓αcdr␈↓,␈α
etc,␈α
and␈αvariables␈α
like␈α
␈↓αt␈↓␈α
and␈α
␈↓αnil␈↓.␈α A
␈↓ ↓H␈↓useful␈α∂interpretation␈α∂of␈α∂global␈α∂variables␈α∂is␈α∂that␈α∂they␈α∂are␈α∂bound␈α∂in␈α∂the␈α∂initial␈α∂symbol␈α⊂table,␈α∂also
␈↓ ↓H␈↓called␈α
the␈α␈↓↓global␈α
table␈↓␈↓π 57␈↓.␈α Non-local␈α
variables␈αwhich␈α
are␈αλ-bound␈α
somewhere␈αin␈α
the␈α
symbol␈αtable
␈↓ ↓H␈↓we␈α
call␈α
␈↓↓free␈α
variables␈↓,␈α∞and␈α
variables␈α
which␈α
have␈α
␈↓↓some␈↓␈α∞accessible␈α
binding␈α
at␈α
the␈α
current␈α∞point␈α
in
␈↓ ↓H␈↓the computation are called ␈↓↓bound variables␈↓␈↓π 58␈↓.

␈↓ ↓H␈↓Finally␈α∃the␈α∃first␈α∃argument␈α∃to␈α∃␈↓αequal␈↓␈α∃is␈α∃evaluated␈α∃giving␈α∃␈↓α((A . B) . A)␈↓.␈α∃ As␈α∃we␈α⊗complete␈α∃that
␈↓ ↓H␈↓evaluation␈αthe␈αlocal␈αbinding␈αfor␈α␈↓αx␈↓␈αis␈αremoved,␈αand␈α␈↓αy␈↓␈αbecomes␈αlocal␈αagain.␈αWe␈αexamine␈α
the␈αsecond

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 57␈↓␈α
This␈α
analogy␈α
breaks␈α
down␈α
somewhat␈α
in␈α
that␈α
usual␈α
implementations␈α
of␈α
LISP␈α
allow␈α
this␈αglobal
␈↓ ↓H␈↓table␈α⊂to␈α⊂be␈α∂augmented;␈α⊂for␈α⊂example,␈α∂by␈α⊂function␈α⊂definitions␈α∂using␈α⊂a␈α⊂version␈α∂of␈α⊂"<=".␈α⊂Thus␈α∂the
␈↓ ↓H␈↓global table can be enlarged whereas a true λ-binding involves a fixed number of variables.

␈↓ ↓H␈↓␈↓π 58␈↓␈α
Our␈αnotion␈α
of␈α
free␈αand␈α
bound␈αvariables␈α
has␈α
a␈αdecidedly␈α
computational␈α
flavor,␈αin␈α
contrast␈αto␈α
the
␈↓ ↓H␈↓mathematical definitions of "free" and "bound".
␈↓ ↓H␈↓␈↓↓112  Evaluation␈↓ 24.7␈↓


␈↓ ↓H␈↓argument␈αto␈α␈↓αequal␈↓,␈α␈↓αx␈↓,␈αand␈α
find␈αthere␈αis␈αnow␈αno␈αbinding␈α
for␈αthat␈αvariable.␈αVariables␈αwhich␈αhave␈α
no
␈↓ ↓H␈↓binding␈αof␈αany␈αkind␈αat␈αthe␈αtime␈αwe␈α
ask␈αfor␈αa␈αvalue␈αare␈αcalled␈α␈↓↓unbound␈αvariables␈↓.␈αThe␈α
local,␈αfree,
␈↓ ↓H␈↓and global variables make up the class of ␈↓↓bound variables␈↓.

␈↓ ↓H␈↓For␈α∂a␈α⊂computation␈α∂to␈α⊂be␈α∂meaningful,␈α⊂each␈α∂variable␈α⊂which␈α∂that␈α⊂computation␈α∂references␈α⊂must␈α∂be
␈↓ ↓H␈↓bound␈α∞␈↓↓somewhere␈↓␈α
when␈α∞we␈α
ask␈α∞for␈α
its␈α∞value.␈α
So␈α∞the␈α
computation␈α∞of␈α
our␈α∞current␈α∞example␈α
would
␈↓ ↓H␈↓fail.␈α
Since␈αwe␈α
are␈α
doing␈αcall-by-value,␈α
it␈α
would␈αfail␈α
even␈α
before␈αwe␈α
asked␈α
for␈αthe␈α
definition␈αof␈α
␈↓αequal␈↓.
␈↓ ↓H␈↓One of our tasks will be to discuss where definitions such as that for ␈↓αequal␈↓ should be kept.

␈↓ ↓H␈↓Here is a diagram of our characterization of variables:
␈↓"␈↓ ↓H␈↓∂                            ␈↓variables␈↓∂
␈↓"␈↓ ↓H␈↓∂                               ~
␈↓"␈↓ ↓H␈↓∂                       ⊂ααααααα∀ααααααα⊃
␈↓"␈↓ ↓H␈↓∂                       ~               ~
␈↓"␈↓ ↓H␈↓∂                     ␈↓non-local␈↓∂          ␈↓local␈↓∂
␈↓"␈↓ ↓H␈↓∂                       ~
␈↓"␈↓ ↓H␈↓∂                    ⊂αα∀ααααααπααααααααααα⊃
␈↓"␈↓ ↓H␈↓∂                    ~         ~           ~
␈↓"␈↓ ↓H␈↓∂                   ␈↓free␈↓∂      ␈↓global␈↓∂      ␈↓unbound␈↓∂

␈↓ ↓H␈↓Notice␈α
that␈α
a␈α
variable␈α
which␈α
is␈α
initially␈α
global␈α
may␈α
become␈α
local␈α
and␈α
then␈α
non-local␈α
by␈α
virtue␈αof
␈↓ ↓H␈↓λ-bindings.

␈↓ ↓H␈↓One␈α⊃of␈α∩the␈α⊃difficulties␈α⊃in␈α∩programming␈α⊃languages␈α⊃is␈α∩deciding␈α⊃what␈α⊃value␈α∩to␈α⊃associate␈α∩with␈α⊃a
␈↓ ↓H␈↓non-local␈αvariable.␈α In␈αLISP,␈αit␈αis␈αclear␈α␈↓↓how␈↓␈αvalues␈αget␈αassociated;␈αit␈αhappens␈αthrough␈αλ-binding␈αor
␈↓ ↓H␈↓by␈α⊃virtue␈α⊃of␈α⊃an␈α⊃initial␈α⊃entry␈α⊃in␈α⊃the␈α⊂symbol␈α⊃table.␈α⊃ The␈α⊃binding␈α⊃strategy␈α⊃for␈α⊃local␈α⊃variables␈α⊂is
␈↓ ↓H␈↓reasonably␈α
uniform␈α
in␈α
programming␈αlanguages:␈α
bind␈α
some␈α
form␈αof␈α
the␈α
actual␈α
parameter␈↓π 59␈↓␈α
to␈αthe
␈↓ ↓H␈↓formal␈α
parameter␈αand␈α
evaluate␈αthe␈α
body␈α
of␈αthe␈α
definition.␈α The␈α
difficulty␈αis␈α
that␈α
frequently␈αthere
␈↓ ↓H␈↓will␈α
be␈α
choices␈α
of␈α
values␈αto␈α
associate.␈α
The␈α
scheme␈α
which␈α
LISP␈αuses␈α
for␈α
discovering␈α
the␈α
value␈αof␈α
any
␈↓ ↓H␈↓variable␈αis␈αto␈αproceed␈αlinearly␈αdown␈αthe␈αsymbol␈αtable,␈αlooking␈αfor␈αthe␈α␈↓↓latest␈↓␈αbinding.␈α This␈αscheme
␈↓ ↓H␈↓is␈α∂called␈α∂␈↓↓dynamic␈α∂binding␈↓.␈α∂It␈α∂␈↓↓usually␈↓␈α∂results␈α∂in␈α∂uncovering␈α∂the␈α∂value␈α∂that␈α∂is␈α∂expected;␈α∂but␈α∂not
␈↓ ↓H␈↓always.␈α Conceptually,␈αthe␈αdynamic␈αbinding␈αscheme␈αcorresponds␈αto␈αthe␈αphysical␈αreplacement␈αof␈αthe
␈↓ ↓H␈↓function call with the function body and then an evaluation of the resulting expression.

␈↓ ↓H␈↓In␈α
review,␈αthe␈α
evaluation␈αof␈α
a␈α
typical␈αfunction-call␈α
will␈αinvolve␈α
the␈α
evaluation␈αof␈α
the␈αarguments,␈α
the
␈↓ ↓H␈↓binding␈α
of␈αthe␈α
λ-variables␈α
to␈αthose␈α
values,␈α
the␈αaddition␈α
of␈α
these␈αnew␈α
bindings␈α
to␈αthe␈α
front␈α
of␈αthe
␈↓ ↓H␈↓symbol␈αtable,␈αand␈αfinally␈αthe␈αevaluation␈αof␈αthe␈αbody␈αof␈αthe␈αfunction.␈α That␈αsegment␈αof␈α
the␈αsymbol
␈↓ ↓H␈↓table␈α∞which␈α∞we␈α∞have␈α∞just␈α∞added␈α∞by␈α∞the␈α∞λ-binding␈α∞will␈α∞be␈α∞called␈α∞the␈α∞␈↓↓local␈α∞symbol␈α∞table␈↓␈α∞or␈α
local
␈↓ ↓H␈↓environment.␈αThe␈αvariables␈αwhich␈αappear␈αin␈αthat␈αsegment␈αare␈αthe␈αlocal␈αvariables.␈α The␈αremainder
␈↓ ↓H␈↓of␈αthe␈αsymbol␈αtable␈αmakes␈αup␈αthe␈α␈↓↓non-local␈αtable␈↓.␈α Variables␈αwhich␈αappear␈αin␈αthe␈αglobal␈αtable␈αbut
␈↓ ↓H␈↓not␈α
in␈αany␈α
local␈αtable␈α
are␈αthe␈α
␈↓↓global␈αvariables␈↓.␈α
 Free␈αvariables␈α
are␈αbound␈α
somewhere␈α
between␈αthe
␈↓ ↓H␈↓local␈α
table␈α∞and␈α
the␈α∞global␈α
table.␈α
 Variables␈α∞which␈α
are␈α∞local␈α
to␈α
a␈α∞form-evaluation␈α
are␈α∞those␈α
which

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 59␈↓ evaluated or unevaluated
␈↓ ↓H␈↓␈↓↓4.7␈↓ 	{Variables     113␈↓


␈↓ ↓H␈↓were␈αpresent␈αin␈αthe␈αλ-binding.␈α We␈αfirst␈αwish␈αto␈αdevelop␈αa␈αuseful␈αnotation␈αfor␈αdescribing␈αbindings
␈↓ ↓H␈↓before␈αdelving␈α
further␈αinto␈α
the␈αintricacies␈α
of␈αbinding␈α
strategies.␈α That␈α
discussion␈αwill␈α
be␈αthe␈α
content
␈↓ ↓H␈↓of Section 4.11.

␈↓ ↓H␈↓↓␈↓ ε⊃Problems

␈↓ ↓H␈↓I␈α⊂Write␈α⊂a␈α∂LISP␈α⊂predicate,␈α⊂␈↓αnon␈α∂<=␈α⊂λ[[x;e]␈α⊂...␈α∂]␈↓,␈α⊂which␈α⊂will␈α∂give␈α⊂␈↓
t␈↓␈α⊂just␈α∂in␈α⊂the␈α⊂case␈α∂that␈α⊂␈↓αx␈↓␈α⊂and␈α∂␈↓αe␈↓
␈↓ ↓H␈↓represent a variable and a λ-expression respectively, and ␈↓αx␈↓ is non-local to ␈↓αe␈↓.



␈↓ ↓H␈↓␈↓ ∧w␈↓↓4.8  Environments and bindings␈↓


␈↓ ↓H␈↓This␈α
section␈α
will␈α
introduce␈α
one␈α
more␈αnotation␈α
for␈α
describing␈α
symbol␈α
tables␈α
or␈α
environments.␈αThis
␈↓ ↓H␈↓notation,␈α_due␈α→to␈α_J.␈α→Weizenbaum,␈α_only␈α→shows␈α_the␈α→abstract␈α_structure␈α→of␈α_the␈α→symbol␈α_table
␈↓ ↓H␈↓manipulations␈αduring␈α
evaluation.␈αIts␈αsimplicity␈α
will␈αbe␈αof␈α
great␈αbenefit␈αwhen␈α
we␈αintroduce␈αthe␈α
more
␈↓ ↓H␈↓complex binding schemes necessary for function-valued functions in Section 4.10.

␈↓ ↓H␈↓In␈α
the␈αprevious␈α
discussions␈αit␈α
has␈αbeen␈α
sufficient␈αsimply␈α
to␈α
think␈αof␈α
a␈αsymbol␈α
table␈αas␈α
a␈αsequence␈α
of
␈↓ ↓H␈↓pairs;␈αeach␈α
pair␈αwas␈α
a␈αvariable␈α
and␈αits␈αassociated␈α
value.␈αThis␈α
sufficed␈αbecause␈α
we␈αdealt␈α
only␈αwith
␈↓ ↓H␈↓λ-variables;␈αwe␈αignored␈αthe␈αpossibility␈αof␈αfree␈αvariables.␈αAs␈αlong␈αas␈αwe␈αadded␈αthe␈αλ-bindings␈αto␈αthe
␈↓ ↓H␈↓␈↓↓front␈↓␈α∂of␈α∂the␈α∂sequence␈α∂representing␈α∂the␈α∂symbol␈α∂table␈α∂we␈α∂showed␈α∂that␈α∂expected␈α∂evaluation␈α∞would
␈↓ ↓H␈↓result.␈α
Local␈α
values␈α
were␈α
found␈α
in␈α
the␈α
table;␈α
global␈α
values␈α
were␈α
found␈α
by␈α
explicit␈α
recognizers␈αin␈α
␈↓αeval␈↓
␈↓ ↓H␈↓and␈α
␈↓αapply␈↓.␈α With␈α
the␈α
advent␈αof␈α
free␈αvariables,␈α
however␈α
it␈αis␈α
convenient,␈α
and␈αsoon␈α
will␈αbe␈α
necessary,
␈↓ ↓H␈↓to␈α
examine␈α
the␈αstructure␈α
of␈α
environments␈αmore␈α
closely.␈α
 We␈α
will␈αdescribe␈α
our␈α
environments␈αin␈α
terms
␈↓ ↓H␈↓of a local symbol table augmented by a description of where to look for the non-local values.

␈↓ ↓H␈↓Instead␈αof␈αhaving␈α
one␈αamorphous␈αsequential␈α
symbol␈αtable,␈αwe␈α
envision␈αa␈αsequence␈α
of␈αtables.␈αOne␈α
is
␈↓ ↓H␈↓the␈α
local␈αtable,␈α
and␈α
its␈αsuccessor␈α
in␈αthe␈α
sequence␈α
is␈αthe␈α
previous␈α
local␈αtable.␈α
 The␈αinformation␈α
telling
␈↓ ↓H␈↓where␈α∂to␈α⊂find␈α∂the␈α⊂previous␈α∂table␈α∂is␈α⊂called␈α∂the␈α⊂␈↓↓access␈α∂chain␈↓␈α∂or␈α⊂␈↓↓access␈α∂link␈↓.␈α⊂ Thus␈α∂if␈α⊂tables␈α∂are
␈↓ ↓H␈↓represented by E␈↓βi␈↓ and the access link by ␈↓→␈↓ then we might represent a symbol table as:
␈↓ ↓H␈↓␈↓ ¬_␈↓α(␈↓E␈↓βn␈↓ → ␈↓E␈↓βn-1␈↓ → ␈↓ ...␈↓→ ␈↓E␈↓β1 ␈↓→␈↓ E␈↓β0␈↓α)

␈↓ ↓H␈↓where E␈↓βn␈↓ is the local or current segment of the table.  We reserve E␈↓β0␈↓ to name the global table.

␈↓ ↓H␈↓LISP␈α∂thus␈α∂finds␈α∞local␈α∂bindings␈α∂in␈α∞the␈α∂local␈α∂table␈α∂and␈α∞uses␈α∂the␈α∂access␈α∞chain␈α∂to␈α∂find␈α∂bindings␈α∞of
␈↓ ↓H␈↓non-local variables.  If a variable is not found in any of the tables, then it is unbound.

␈↓ ↓H␈↓Now to establish some notation.  An environment will be described as :
␈↓ ↓H␈↓␈↓↓114  Evaluation␈↓ 14.8␈↓


␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8␈↓	Form␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8E␈↓βlocal␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8| E␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬x_________
␈↓ ↓H␈↓␈↓ ¬xvar␈↓ ε8| value
␈↓ ↓H␈↓␈↓ ¬x␈↓αv␈↓β1␈↓␈↓ ε8| ␈↓	val␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓αv␈↓β2␈↓␈↓ ε8| ␈↓	val␈↓β2␈↓
␈↓ ↓H␈↓␈↓ ¬x    .....
␈↓ ↓H␈↓␈↓ ¬x␈↓αv␈↓βn␈↓␈↓ ε8| ␈↓	val␈↓βn␈↓
␈↓ ↓H␈↓␈↓ ¬x␈↓ ε8|


␈↓ ↓H␈↓␈↓	Form␈↓␈α⊂is␈α⊂the␈α⊂current␈α⊂form␈α⊂being␈α⊂evaluated.␈α⊂ E␈↓βlocal␈↓␈α⊂is␈α⊂the␈α⊂name␈α⊂of␈α⊂the␈α⊂current␈α⊃environment␈α⊂or
␈↓ ↓H␈↓symbol␈α
table.␈α
Let␈α
␈↓αx␈↓␈α
be␈α
a␈α
variable␈α
appearing␈α
in␈α␈↓	Form␈↓.␈α
If␈α
␈↓αx␈↓␈α
is␈α
not␈α
found␈α
among␈α
the␈α
␈↓αv␈↓βj␈↓'s,␈αthen␈α
entries
␈↓ ↓H␈↓in␈α∂the␈α∂table␈α∂named␈α⊂E␈↓βi␈↓␈α∂are␈α∂examined.␈α∂ If␈α⊂the␈α∂variable␈α∂is␈α∂not␈α⊂found␈α∂in␈α∂E␈↓βi␈↓␈α∂then␈α⊂the␈α∂environment
␈↓ ↓H␈↓mentioned␈α∞in␈α∞the␈α∞upper␈α∞right-hand␈α∞quadrant␈α∞of␈α∂E␈↓βi␈↓␈α∞is␈α∞searched.␈α∞The␈α∞search␈α∞will␈α∞terminate␈α∂if␈α∞the
␈↓ ↓H␈↓variable␈α∞is␈α∞found;␈α∞the␈α∞value␈α∞is␈α∞then␈α∞the␈α∞corresponding␈α∞␈↓	val␈↓βj␈↓.␈α∞ If␈α∞␈↓αx␈↓␈α∞is␈α∞not␈α∞found␈α∞locally,␈α∞and␈α
the
␈↓ ↓H␈↓symbol "/" appears in the right-hand quadrant, then ␈↓αx␈↓ is unbound.

␈↓ ↓H␈↓The␈αnotation␈αis␈αused␈αas␈αfollows:␈αwhen␈αwe␈αbegin␈αthe␈αevaluation␈αof␈αa␈αform,␈αthe␈αinitial␈αtable␈αE␈↓β0␈↓␈αis␈αset
␈↓ ↓H␈↓up␈α
with␈α∞"/"␈α
in␈α
its␈α∞access␈α
field.␈α
The␈α∞execution␈α
of␈α
a␈α∞function␈α
definition,␈α
say␈α∞␈↓αf <= λ[[x;y]x␈↓π2␈↓α + y]␈↓,␈α
will
␈↓ ↓H␈↓add␈α∞an␈α∂appropriate␈α∞entry␈α∂to␈α∞the␈α∂table,␈α∞binding␈α∂␈↓αf␈↓␈α∞to␈α∂its␈α∞lambda␈α∂definition␈↓π 60␈↓.␈α∞ Now,␈α∂consider␈α∞the
␈↓ ↓H␈↓evaluation␈αof␈αthe␈αform␈α␈↓αf[2;3]␈↓.␈α When␈αthe␈αλ-expression␈αis␈αentered,␈αi.e.,␈αwhen␈αwe␈αbind␈αthe␈αevaluated
␈↓ ↓H␈↓arguments␈α
(␈↓α2␈↓␈αand␈α
␈↓α3␈↓)␈α
to␈αthe␈α
λ-variables␈α
(␈↓αx␈↓␈αand␈α
␈↓αy␈↓),␈α
a␈αnew␈α
local␈α
table␈α(E␈↓β1␈↓)␈α
is␈α
set␈αup␈α
with␈α
an␈αaccess␈α
link
␈↓ ↓H␈↓to␈α∂E␈↓β0␈↓.␈α⊂ Entries␈α∂reflecting␈α∂the␈α⊂binding␈α∂of␈α⊂the␈α∂λ-variables␈α∂are␈α⊂made␈α∂in␈α∂E␈↓β1␈↓␈α⊂and␈α∂evaluation␈α⊂of␈α∂the
␈↓ ↓H␈↓λ-body is begun.
␈↓ ↓H␈↓The flow of symbol table creation is:

␈↓ ↓H␈↓␈↓ β(␈↓ βX␈↓αf[2;3]␈↓ ¬x␈↓ ε(x␈↓π2␈↓α + y␈↓
␈↓ ↓H␈↓␈↓ β(␈↓ βXE␈↓β0␈↓␈↓ ∧λ␈↓ ¬x␈↓ ε(E␈↓β1␈↓␈↓ λ_␈↓ λh␈↓ 	_E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ β(␈↓ βX| /␈↓ ∧λ␈↓ ¬x␈↓ ε(| E␈↓β0␈↓␈↓ λ_␈↓ λh␈↓ 	_| /
␈↓ ↓H␈↓␈↓ β(␈↓ βX______ =>␈↓ ¬x______␈↓ λ_=>␈↓ λh______
␈↓ ↓H␈↓␈↓ β( ␈↓αf␈↓ βX| λ[[x;y]x␈↓π2␈↓α + y]␈↓␈↓ ¬x␈↓αx␈↓ ε(| 2␈↓ λ_␈↓ λhf␈↓ 	_| λ[[x;y] ...
␈↓ ↓H␈↓α␈↓ β(␈↓ βX␈↓ ∧λ␈↓ ¬x y␈↓ ε(| 3␈↓

␈↓ ↓H␈↓Compare this sequence to the example on page 104.







␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 60␈↓␈αNote␈αthat␈αwe␈αreally␈αmean␈α"representation␈αof␈αlambda␈αdefinition".␈αHowever␈αthe␈αinformal␈αnotation
␈↓ ↓H␈↓is easier to read and thus we will continue the deception.
␈↓ ↓H␈↓␈↓↓4.8␈↓ λαEnvironments and bindings     115␈↓



␈↓ ↓H␈↓The sequence of tables corresponds to the evaluation sequence:
␈↓ ↓H␈↓␈↓ ∧*␈↓αeval[␈↓
R␈↓∞( ␈↓αf[2;3] ␈↓∞)␈↓α; ␈↓
R␈↓∞( ␈↓α{<f , λ[[x;y] x␈↓π2␈↓α +y]>} ␈↓∞)␈↓α]
␈↓ ↓H␈↓α␈↓ εH↓
␈↓ ↓H␈↓α␈↓ βMeval[␈↓
R␈↓∞( ␈↓αx␈↓π2␈↓α + y ␈↓∞)␈↓α; ␈↓
R␈↓∞( ␈↓α{<x, 2>, <y, 3>, <f , λ[[x;y] x␈↓π2␈↓α +y]>} ␈↓∞)␈↓α]
␈↓ ↓H␈↓α␈↓ εH↓
␈↓ ↓H␈↓α␈↓ εI7␈↓
␈↓ ↓H␈↓␈↓↓116  Evaluation␈↓ 14.8␈↓



␈↓ ↓H␈↓The␈α∀execution␈α∪of␈α∀␈↓αfact[3]␈↓␈α∀on␈α∪page 104␈α∀results␈α∪in␈α∀a␈α∀more␈α∪interesting␈α∀example.␈α∀The␈α∪following
␈↓ ↓H␈↓discussion should be read in conjunction with that description␈↓π 61␈↓.
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
εα→ ␈↓α6␈↓

␈↓"␈↓ ↓H␈↓
␈↓ αH␈↓ β_␈↓αfact[3]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→ ...]␈↓ εx␈↓ πH␈↓ λ_*[x;fact[x-1]]␈↓ 	H␈↓
ε←⊃␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓E␈↓β0␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β1␈↓␈↓ εx␈↓ πH␈↓ λ_E␈↓β1␈↓␈↓ 	H␈↓
~ ~␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓ β_| /␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β0␈↓␈↓ εx␈↓ πH␈↓ λ_|  E␈↓β0␈↓␈↓ 	H␈↓
$ ↑␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=>␈↓ πH_______ =>␈↓ 	H␈↓ 	h␈↓α2␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓αfact␈↓ β_| λ[[x][x=0→1;...]␈↓ ¬λ x␈↓ ¬X| 3␈↓ εx␈↓ πH x␈↓ λ_| 3␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃ ~␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
ε→$␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_fact[2]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→ ...]␈↓ εx␈↓ πH␈↓ λ_*[x;fact[x-1]]␈↓ 	H␈↓
~␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓E␈↓β1␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β2␈↓␈↓ εx␈↓ πH␈↓ λ_E␈↓β2␈↓␈↓ 	H␈↓
ε←⊃␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓ β_| E␈↓β0␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β1␈↓␈↓ εx␈↓ πH␈↓ λ_|  E␈↓β1␈↓␈↓ 	H␈↓
$ ↑␈↓
␈↓"␈↓ ↓H␈↓  =>␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=>␈↓ πH_______ =>␈↓ 	H␈↓ 	h␈↓α1␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓α  x␈↓ β_| 3␈↓ ∧8␈↓ ¬λ x␈↓ ¬X| 2␈↓ εx␈↓ πH x␈↓ λ_| 2␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h␈↓
~␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃ ~␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
ε→$␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_fact[1]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→ ...]␈↓ εx␈↓ πH␈↓ λ_*[x;fact[x-1]]␈↓ 	H␈↓
~␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓E␈↓β2␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β3␈↓␈↓ εx␈↓ πH␈↓ λ_E␈↓β3␈↓␈↓ 	H␈↓
ε←⊃␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓ β_| E␈↓β1␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β2␈↓␈↓ εx␈↓ πH␈↓ λ_|  E␈↓β2␈↓␈↓ 	H␈↓
$ ~␈↓
␈↓"␈↓ ↓H␈↓  =>␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=>␈↓ πH_______ =>␈↓ 	H␈↓ 	h␈↓
~␈↓
␈↓"␈↓ ↓H␈↓␈↓ αH␈↓α  x␈↓ β_| 2␈↓ ∧8␈↓ ¬λ x␈↓ ¬X| 1␈↓ εx␈↓ πH x␈↓ λ_| 1␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h1
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h␈↓
↑␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓ ∧8␈↓ ¬λ␈↓ ¬X␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓ 	h␈↓
~␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_fact[0]␈↓ ∧8␈↓ ¬λ␈↓ ¬X[x=0→1; ...]␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
⊃ ~␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_E␈↓β3␈↓α␈↓ ∧8␈↓ ¬λ␈↓ ¬XE␈↓β4␈↓α␈↓ εx␈↓ πH␈↓ λ_␈↓ 	H␈↓
~ ↑␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH␈↓ β_| E␈↓β2␈↓α␈↓ ∧8␈↓ ¬λ␈↓ ¬X|  E␈↓β3␈↓α␈↓ εx␈↓ πH␈↓ λ_␈↓send␈↓α␈↓ 	H␈↓
~ ~␈↓α
␈↓"␈↓ ↓H␈↓α  =>␈↓ αH_______␈↓ ∧8=>␈↓ ¬λ_______␈↓ εx=> 1␈↓ πH␈↓ λ_ 1␈↓ 	H␈↓
ε→$␈↓α
␈↓"␈↓ ↓H␈↓α␈↓ αH  x␈↓ β_| 1␈↓ ∧8␈↓ ¬λ x␈↓ ¬X| 0␈↓ εx␈↓ πH␈↓ λ_␈↓back up␈↓ 	H␈↓
$␈↓


␈↓ ↓H␈↓At␈α∞the␈α∞end␈α∞of␈α∞the␈α∞first␈α∞line␈α∞we␈α∞are␈α∞faced␈α∞with␈α∞the␈α∞evaluation␈α∞of␈α∞␈↓α*[x;fact[x-1]]␈↓.␈α∞This␈α∞requires␈α∞the
␈↓ ↓H␈↓evaluation␈α
of␈αthe␈α
arguments␈αto␈α
*;␈α
this␈αis␈α
done␈αby␈α
␈↓αevlis␈↓␈α
and␈αrequires␈α
the␈αevaluation␈α
of␈α␈↓αfact[x-1]␈↓␈α
using
␈↓ ↓H␈↓the␈αenvironment␈αE␈↓β1␈↓.␈αIn␈αE␈↓β1␈↓␈α␈↓αx-1␈↓␈αgives␈α␈↓α2␈↓,␈αand␈αwe␈αfind␈αthe␈αdefinition␈αof␈α␈↓αfact␈↓␈αin␈αE␈↓β0␈↓.␈αIn␈αline␈αtwo␈αwe␈αset
␈↓ ↓H␈↓up␈α∞E␈↓β2␈↓␈α∞and␈α∞evaluate␈α∂␈↓αfact[2]␈↓.␈α∞Analogous␈α∞situations␈α∞occur␈α∞until␈α∂line␈α∞four;␈α∞at␈α∞this␈α∞time␈α∂we␈α∞suddenly
␈↓ ↓H␈↓find␈α
ourselves␈α
in␈α
E␈↓β4␈↓␈α
with␈α
␈↓αx␈↓␈α
bound␈α
to␈α
␈↓α0␈↓.␈αThe␈α
predicate␈α
␈↓αx=0␈↓␈α
is␈α
satisfied␈α
and␈α
we␈α
start␈α
back␈α
up␈αthe
␈↓ ↓H␈↓right␈αmargin␈αto␈αconclude␈αthe␈αnested␈αevaluations␈αof␈α␈↓α*[x;fact[x-1]]␈↓.␈αThis␈αprocess␈αfinally␈αterminates␈αat
␈↓ ↓H␈↓the top, returning a value ␈↓α6␈↓.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 61␈↓ The layout of this example is due to R. Davis.
␈↓ ↓H␈↓␈↓↓4.8␈↓ λαEnvironments and bindings     117␈↓


␈↓ ↓H␈↓Notice␈αin␈αthis␈αexample␈αthat␈αwe␈αwill␈αget␈αthe␈α
correct␈αbinding␈αof␈α␈↓αx␈↓␈αlocally.␈α It␈αis␈αimportant␈αto␈αnote␈α
that
␈↓ ↓H␈↓the␈α⊂occurrence␈α∂of␈α⊂␈↓αfact␈↓␈α∂within␈α⊂the␈α∂body␈α⊂of␈α∂the␈α⊂definition␈α∂of␈α⊂␈↓αfact␈↓␈α∂is␈α⊂␈↓↓global␈↓.␈α∂We␈α⊂find␈α⊂the␈α∂correct
␈↓ ↓H␈↓binding␈αfor␈α␈↓αfact␈↓␈αby␈αsearching␈αthe␈αaccess␈αchain.␈α We␈αmust␈αsearch␈αthe␈αaccess␈αchain␈αeven␈αthough␈α␈↓αfact␈↓
␈↓ ↓H␈↓is␈α∞global.␈α∞ We␈α∞cannot␈α∞shortcut␈α∂the␈α∞search␈α∞by␈α∞simply␈α∞looking␈α∂in␈α∞E␈↓β0␈↓.␈α∞A␈α∞variable␈α∞might␈α∂have␈α∞been
␈↓ ↓H␈↓rebound in an enclosing environment and it would be that binding we should discover.

␈↓ ↓H␈↓As␈α~a␈α~final␈α→example␈α~showing␈α~access␈α→to␈α~non-local␈α~variable␈α→bindings␈α~consider␈α~␈↓αf[3]␈↓␈α→where
␈↓ ↓H␈↓␈↓αf <= λ[[x]g[2]]␈↓ and ␈↓αg <= λ[[y]x+y]␈↓.

␈↓ ↓H␈↓␈↓ βH␈↓ βx␈↓αf[3]␈↓ ∧H␈↓ ε_␈↓ ε8g[2]␈↓ λ8␈↓ 	λ␈↓ 	8x + y     ....
␈↓ ↓H␈↓α␈↓ βH␈↓ βxE␈↓β0␈↓α␈↓ ∧H␈↓ ε_␈↓ ε8E␈↓β1␈↓α␈↓ λ8␈↓ 	λ␈↓ 	8E␈↓β2␈↓α             ....
␈↓ ↓H␈↓α␈↓ βH␈↓ βx| /␈↓ ∧H␈↓ ε_␈↓ ε8| E␈↓β0␈↓α␈↓ λ8␈↓ 	λ␈↓ 	8| E␈↓β1␈↓α
␈↓ ↓H␈↓α␈↓ βH______␈↓ ∧H     =>␈↓ ε_______␈↓ λ8=>␈↓ 	λ______␈↓ 
λ           ......
␈↓ ↓H␈↓α␈↓ βH  f␈↓ βx| λ[[x] g[2]]␈↓ ε_x␈↓ ε8| 3␈↓ λ8␈↓ 	λy␈↓ 	8| 2       ...
␈↓ ↓H␈↓α␈↓ βH  g␈↓ βx| λ[[y] x+y]

␈↓ ↓H␈↓Notice␈αthat␈αwhen␈αwe␈αattempt␈αto␈αevaluate␈α␈↓αx␈α+␈αy␈↓␈αwe␈αfind␈α␈↓αy␈↓␈αhas␈αa␈αlocal␈αvalue,␈αbut␈αwe␈αmust␈αlook␈αdown
␈↓ ↓H␈↓the access chain to find a binding for ␈↓αx␈↓.

␈↓ ↓H␈↓The scheme for using Weizenbaum environments for the current LISP subset is:

␈↓ ↓H␈↓␈↓ α_When␈αdoing␈αa␈αλ-binding,␈αset␈αup␈αa␈αnew␈α
E␈↓βnew␈↓␈αwith␈αthe␈αλ-variables␈αas␈αthe␈αlocal␈αvariable␈α
entries
␈↓ ↓H␈↓␈↓ α_and␈α
the␈α
values␈α
of␈α
the␈αarguments␈α
as␈α
the␈α
corresponding␈α
value␈αentries.␈α
 The␈α
access␈α
slot␈α
of␈αthe
␈↓ ↓H␈↓␈↓ α_new␈α∪E␈↓βnew␈↓␈α∩points␈α∪to␈α∪the␈α∩previous␈α∪symbol␈α∩table.␈α∪ The␈α∪evaluation␈α∩of␈α∪the␈α∩body␈α∪of␈α∪the␈α∩λ-
␈↓ ↓H␈↓␈↓ α_expression␈α∞takes␈α∞place␈α∞using␈α
the␈α∞new␈α∞table;␈α∞when␈α
a␈α∞local␈α∞variable␈α∞is␈α
accessed␈α∞we␈α∞find␈α∞it␈α
in
␈↓ ↓H␈↓␈↓ α_E␈↓βnew␈↓; when a non-local or global variable occurs, we chase the access chain to find its value.

␈↓ ↓H␈↓␈↓ α_When␈α_the␈α_evaluation␈α_of␈α↔the␈α_body␈α_is␈α_completed,␈α↔E␈↓βnew␈↓␈α_disappears␈α_and␈α_the␈α↔previous
␈↓ ↓H␈↓␈↓ α_environment is restored.

␈↓ ↓H␈↓You␈α∂should␈α∂convince␈α∂yourself␈α∂that␈α∂the␈α∂current␈α∂access-␈α∂and␈α∂binding-scheme␈α∂espoused␈α∂by␈α∂LISP␈α∞is
␈↓ ↓H␈↓faithfully described in these diagrams.



␈↓ ↓H␈↓␈↓ ε↔␈↓↓Problem␈↓


␈↓ ↓H␈↓␈↓↓1.␈↓␈α
By␈αnow␈α
you␈αshould␈α
realize␈αthat␈α
environments␈α
really␈αare␈α
a␈αclass␈α
of␈αabstract␈α
data␈α
structures:␈αthey
␈↓ ↓H␈↓should␈αhave␈αconstructors,␈αselectors,␈αand␈αrecognizers.␈α To␈αhelp␈αdiscover␈αwhat␈αa␈αset␈αof␈αsuch␈αfunctions
␈↓ ↓H␈↓might␈α⊃be,␈α⊃give␈α⊃a␈α⊂representation␈α⊃for␈α⊃Weizenbaum␈α⊃environments␈α⊂and␈α⊃write␈α⊃new␈α⊃versions␈α⊃of␈α⊂the
␈↓ ↓H␈↓symbol␈α∪table␈α∪manipulating␈α∪functions,␈α∪␈↓αlookup␈↓␈α∪and␈α∪␈↓αmkenv␈↓,␈α∪which␈α∪will␈α∪operate␈α∀on␈α∪Weizenbaum
␈↓ ↓H␈↓environments.  See page 106.
␈↓ ↓H␈↓␈↓↓118  Evaluation␈↓ 24.9␈↓


␈↓ ↓H␈↓␈↓ ε⊗␈↓↓4.9  ␈↓αlabel␈↓↓␈↓α


␈↓ ↓H␈↓One␈α⊃effect␈α⊃of␈α⊂placing␈α⊃"λ"␈α⊃and␈α⊃a␈α⊂list␈α⊃of␈α⊃λ-variables␈α⊂in␈α⊃front␈α⊃of␈α⊃an␈α⊂expresson␈α⊃is␈α⊃to␈α⊃bind␈α⊂those
␈↓ ↓H␈↓variables␈αwhich␈αappear␈αin␈αthe␈αλ-list.␈αAll␈αother␈αvariables␈αappearing␈αin␈αthe␈αexpression␈αare␈αnon-local.
␈↓ ↓H␈↓For example, ␈↓αf␈↓ is non-local in the following:

␈↓ ↓H␈↓␈↓ ∧L␈↓αf <= λ[[x][zerop[x] → 1; ␈↓
t␈↓α → *[x;f[x-1]]] ].␈↓ 

␈↓ ↓H␈↓Clearly␈αour␈αintention␈αis␈αthat␈αthe␈α␈↓αf␈↓␈αappearing␈αto␈αthe␈αright␈αof␈α"<="␈αis␈αthe␈αsame␈αas␈αthe␈α␈↓αf␈↓␈αappearing␈αto
␈↓ ↓H␈↓the left of "<=".

␈↓ ↓H␈↓This␈αhas␈αnot␈αbeen␈αa␈αproblem␈αfor␈αus.␈α We␈αhave␈αsimply␈αpre-loaded␈αthe␈αsymbol␈αtable,␈αbinding␈α␈↓αf␈↓␈αto␈αits
␈↓ ↓H␈↓definition␈α∞(or␈α∞value);␈α∞see␈α∞page 104.␈α
LISP␈α∞has␈α∞been␈α∞equipped␈α∞with␈α
a␈α∞more␈α∞elegant␈α∞device␈α∞for␈α
this
␈↓ ↓H␈↓binding, called the ␈↓αlabel␈↓ operator. It is used thus:

␈↓ ↓H␈↓␈↓ ¬∃␈↓αlabel[␈↓<identifier>;<function>] 

␈↓ ↓H␈↓and␈α∩has␈α∪the␈α∩effect␈α∪of␈α∩binding␈α∪the␈α∩<identifier>␈α∩to␈α∪the␈α∩<function>.␈α∪ The␈α∩value␈α∪constructed␈α∩by
␈↓ ↓H␈↓executing␈α⊂a␈α⊂␈↓αlabel␈↓-expression␈α∂is␈α⊂a␈α⊂representation␈α∂of␈α⊂a␈α⊂function␈α∂with␈α⊂name␈α⊂<identifier>␈α⊂and␈α∂body
␈↓ ↓H␈↓<function>.  For example, a proper definition of ␈↓αfact␈↓ is:

␈↓ ↓H␈↓␈↓ ∧λ␈↓αlabel[fact; λ[[x][eq[x;0] → 1; ␈↓
t␈↓α → *[x;fact[sub1[x]]]]]]␈↓ 

␈↓ ↓H␈↓To include ␈↓αlabel␈↓ in the LISP syntax add:

␈↓ ↓H␈↓␈↓ ∧=<function>::= ␈↓αlabel␈↓[<identifier>;<function>]

␈↓ ↓H␈↓and the S-expr translation of the ␈↓αlabel␈↓ construct should naturally be:

␈↓ ↓H␈↓␈↓ ∧!␈↓
R␈↓∞( ␈↓αlabel[f;fn] ␈↓∞)␈↓α = (LABEL ␈↓
R␈↓∞( ␈↓αf ␈↓∞)␈↓α  ␈↓
R␈↓∞( ␈↓αfn ␈↓∞)␈↓α)␈↓

␈↓ ↓H␈↓Note that ␈↓αlabel␈↓ is a special form, not a call-by-value function.

␈↓ ↓H␈↓A␈α∪typical␈α∩application␈α∪of␈α∩the␈α∪␈↓αlabel␈↓␈α∩construct,␈α∪say␈α∩␈↓αlabel[f;λ[[x]␈↓λx␈↓α[x]]][A]␈↓.␈α∪ results␈α∩in␈α∪the␈α∩following
␈↓ ↓H␈↓environmental picture when we get ready to evalute ␈↓λx␈↓α[x]␈↓:

␈↓ ↓H␈↓␈↓ αλ␈↓αlabel[f;λ[[x]␈↓λx␈↓α[x]]][A]␈↓␈↓ ∧x␈↓ ¬H␈↓λx␈↓α[x]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ | /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=> ...␈↓ ¬H______
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ |␈↓ ∧x␈↓ ¬H␈↓αf␈↓ ¬x| λ[[x]␈↓λx␈↓α[x]]
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλ␈↓ ∧x␈↓ ¬Hx␈↓ ¬x|A

␈↓ ↓H␈↓Notice␈αthat␈αthe␈αdefinition␈αdoes␈αnot␈αappear␈αin␈αthe␈αglobal␈αtable␈αE␈↓β0␈↓;␈αwe␈αuse␈α␈↓αlabel␈↓␈αto␈α
create␈αtemporary
␈↓ ↓H␈↓␈↓↓4.9␈↓ 
@␈↓αlabel␈↓↓     119␈↓α


␈↓ ↓H␈↓function␈α∂definitions.␈α∞ These␈α∂definitions␈α∞disappear␈α∂when␈α∂the␈α∞environment␈α∂in␈α∞which␈α∂the␈α∂␈↓αlabel␈↓␈α∞was
␈↓ ↓H␈↓executed␈αis␈αno␈αlonger␈αaccessible␈α
to␈αthe␈αcomputation.␈α Thus␈αwithin␈α
the␈αevaluation␈αof␈αthe␈αbody␈α␈↓λx␈↓α[x]␈↓␈α
a
␈↓ ↓H␈↓recursive␈α
call␈αon␈α
␈↓αf␈↓␈αwill␈α
refer␈αto␈α
the␈αdefinition␈α
of␈α
␈↓αf␈↓␈αlocated␈α
in␈αE␈↓β1␈↓␈α
so␈αlong␈α
as␈α␈↓αf␈↓␈α
is␈αnot␈α
rebound␈α
in␈α␈↓λx␈↓;
␈↓ ↓H␈↓once␈αwe␈αhave␈α
completed␈αthe␈αcomputation␈α
initialized␈αin␈αE␈↓β0␈↓␈αthe␈α
definition␈αof␈α␈↓αf␈↓␈α
will␈αdisappear.␈α If␈α␈↓αf␈↓␈α
is
␈↓ ↓H␈↓not recursive, then the use of ␈↓αlabel␈↓ is unnecessary; an anonymous function application will suffice.

␈↓ ↓H␈↓What␈α
about␈α∞statements␈α
like␈α
"evaluate␈α∞␈↓αg[A;B]␈↓␈α
where␈α
␈↓αg <= λ[[x;y] ... f[u;v] ...]␈↓␈α∞and␈α
␈↓αf <= λ[[x;y] ... ]␈↓."?
␈↓ ↓H␈↓␈↓αlabel␈↓␈α∪defines␈α∪only␈α∪one␈α∪function;␈α∪we␈α∪may␈α∪not␈α∪say␈α∪␈↓αlabel[f,g; ... ]␈↓.␈α∪ What␈α∪we␈α∪␈↓↓can␈↓␈α∪do␈α∪embed␈α∩the
␈↓ ↓H␈↓␈↓αlabel␈↓-definition for ␈↓αf␈↓ within the ␈↓αlabel␈↓-definition for ␈↓αg␈↓␈↓π 62␈↓. Thus:

␈↓ ↓H␈↓α␈↓ ∧3label[g; λ[[x;y] ... label[f; λ[[x;y] ... ]][u;v] ...]]␈↓

␈↓ ↓H␈↓Implementations␈α∃of␈α⊗LISP␈α∃offer␈α⊗better␈α∃definitional␈α∃facilities,␈α⊗with␈α∃"<="␈α⊗having␈α∃the␈α⊗effect␈α∃of
␈↓ ↓H␈↓permanently establishing the definition in E␈↓β0␈↓.

␈↓ ↓H␈↓The␈αapparent␈αsimplicity␈αof␈αthe␈α␈↓αlabel␈↓␈αoperator␈αis␈αpartly␈αdue␈αto␈αmisconception␈αand␈αpartly␈αdue␈α
to␈αthe
␈↓ ↓H␈↓restrictions␈α∞placed␈α∞on␈α∂the␈α∞current␈α∞subset␈α∂of␈α∞LISP.␈α∞␈↓αlabel␈↓␈α∞appears␈α∂to␈α∞be␈α∞a␈α∂rather␈α∞weak␈α∞form␈α∂of␈α∞an
␈↓ ↓H␈↓assignment␈α∪statement.␈α∩When␈α∪we␈α∩extend␈α∪LISP␈α∩to␈α∪include␈α∩assignments␈α∪we␈α∩can␈α∪show␈α∪that␈α∩such
␈↓ ↓H␈↓interpretation of ␈↓αlabel␈↓ is insufficient.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.  Show one way to change ␈↓αeval␈↓ to handle ␈↓αlabel␈↓.

␈↓ ↓H␈↓II. Express the definition of ␈↓αreverse␈↓ given on page 49 using ␈↓αlabel␈↓.

␈↓ ↓H␈↓III Evaluate the following:

␈↓ ↓H␈↓α␈↓ ¬;λ[[y]label[fn;fn␈↓β2␈↓α][␈↓
f␈↓α]] [␈↓
f␈↓α]

␈↓ ↓H␈↓α␈↓where:␈↓α

␈↓ ↓H␈↓α␈↓ ∧dfn␈↓β2␈↓α <= λ[[x][y → 1; x → 2; ␈↓
t␈↓α → fn␈↓β1␈↓α[␈↓
t␈↓α]]

␈↓ ↓H␈↓α␈↓ ¬←fn␈↓β1␈↓α <= λ[[y]fn[y]]






␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 62␈↓ Indeed ␈↓↓every␈↓ occurrence of ␈↓αf␈↓ must be replaced by the ␈↓αlabel[f;...]␈↓ construct.
␈↓ ↓H␈↓␈↓↓120  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓␈↓ βx␈↓↓4.10  Functional Arguments and Functional Values␈↓


␈↓ ↓H␈↓Recall our discussion of :
␈↓ ↓H␈↓α␈↓ β-eval[(F 2 3);((F . (LAMBDA (X Y) (PLUS (EXPT X 2) Y))))].

␈↓ ↓H␈↓We now know this is equivalent to:
␈↓ ↓H␈↓␈↓ β∧␈↓αeval[((LABEL F (LAMBDA (X Y) (PLUS (EXPT X 2) Y))) 2 3);( )]␈↓.

␈↓ ↓H␈↓In␈αeither␈αcase,␈αthe␈αeffect␈αis␈αto␈αbind␈αthe␈αname␈α␈↓αf␈↓␈αto␈αthe␈αλ-expression.␈α Binding␈αalso␈αoccurs␈αwhen␈α␈↓αf␈↓␈αis
␈↓ ↓H␈↓called:␈α
we␈α
bind␈α
␈↓αx␈↓␈αto␈α
␈↓α2␈↓,␈α
and␈α
␈↓αy␈↓␈αto␈α
␈↓α3␈↓.␈α
 In␈α
the␈α
latter␈αcase␈α
we␈α
are␈α
binding␈αsimple␈α
values;␈α
in␈α
the␈αformer␈α
we
␈↓ ↓H␈↓are␈α
binding␈αfunctions␈α
as␈αvalues.␈α
We␈α
have␈αdecided␈α
that␈αthe␈α
necessary␈αingredients␈α
to␈α
characterize␈αa
␈↓ ↓H␈↓functional␈α∂value␈α⊂␈↓π 63␈↓␈α∂are␈α∂a␈α⊂representation␈α∂of␈α∂the␈α⊂formal␈α∂parameters,␈α∂and␈α⊂a␈α∂representation␈α⊂of␈α∂the
␈↓ ↓H␈↓expression␈αdescribed␈αin␈αthe␈αbody␈αof␈αthe␈αfunction.␈α In␈αthis␈αsection␈αwe␈αwill␈αexamine␈αthe␈αadequacy␈αof
␈↓ ↓H␈↓that decision.  We will proceed informally with a few examples and see what happens.

␈↓ ↓H␈↓Assume␈α∂we␈α∂have␈α∂a␈α∂list␈α∂␈↓αl␈↓␈α∞of␈α∂dotted-pairs␈α∂␈↓λα␈↓β1␈↓ ,..., ␈↓λα␈↓βn␈↓,␈α∂and␈α∂we␈α∂wish␈α∞to␈α∂form␈α∂a␈α∂new␈α∂list␈α∂of␈α∂the␈α∞form
␈↓ ↓H␈↓␈↓α(car[␈↓λα␈↓β1␈↓α] ... car[␈↓λα␈↓βn␈↓α])␈↓.␈α
 That␈α
is␈αwe␈α
wish␈α
to␈αapply␈α
␈↓αcar␈↓␈α
to␈αeach␈α
of␈α
the␈αelements␈α
of␈α
␈↓αl␈↓.␈αSuch␈α
a␈α
function␈αis
␈↓ ↓H␈↓easy to write:

␈↓ ↓H␈↓␈↓ β⊃␈↓αcarfirst <= λ[[l][null[l] → ( ); ␈↓
t␈↓α → concat[car[first[l]];carfirst[rest[l]]]]].␈↓ 

␈↓ ↓H␈↓Now␈αsuppose␈αwe␈αwish␈αto␈αwrite␈αa␈αmore␈αgeneral␈αfunction,␈αwhich␈αinstead␈αof␈αbeing␈αspecific␈αto␈α␈↓αcar␈↓,␈αwill
␈↓ ↓H␈↓take␈α∀an␈α∃␈↓↓arbitrary␈↓␈α∀unary␈α∀function␈α∃␈↓αf␈↓␈α∀and␈α∃apply␈α∀it␈α∀to␈α∃each␈α∀of␈α∀the␈α∃elements␈α∀of␈α∃␈↓αl␈↓,␈α∀generating
␈↓ ↓H␈↓␈↓α(f[␈↓λα␈↓β1␈↓α], ..., f[␈↓λα␈↓βn␈↓α])␈↓.  Such a function could plausibly be defined as follows:

␈↓ ↓H␈↓␈↓ α←␈↓αmapfirst <= λ[[fn;l][null[l] → ( ); ␈↓
t␈↓α → concat[fn[first[l]];mapfirst[fn;rest[l]]]]].␈↓ 

␈↓ ↓H␈↓Thus the first calculation we requested above could be expressed as:

␈↓ ↓H␈↓␈↓ ¬⊂␈↓αmapfirst[car;l]␈↓ .......or could it? 

␈↓ ↓H␈↓Recalling␈α⊂LISP's␈α⊃penchant␈α⊂for␈α⊂call-by-value␈α⊃evaluation,␈α⊂we␈α⊂should␈α⊃realize␈α⊂that␈α⊃the␈α⊂computation
␈↓ ↓H␈↓would␈α
not␈α
be␈αdone␈α
as␈α
expected.␈αWe␈α
do␈α
␈↓↓not␈↓␈α
want␈αthe␈α
argument␈α
␈↓αcar␈↓␈αevaluated.␈α
We␈α
want␈α
its␈α␈↓↓name␈↓.
␈↓ ↓H␈↓We␈αhave␈αseen␈αone␈α
artifact␈αin␈αLISP␈αto␈αsubdue␈α
evaluation:␈αwe␈αcan␈αmake␈α
it␈αa␈αconstant␈αby␈α␈↓αquote␈↓-ing␈α
it.
␈↓ ↓H␈↓Indeed,

␈↓ ↓H␈↓␈↓ ∧π␈↓αmapfirst[quote[car];l] = mapfirst[CAR;l] ␈↓↓will␈↓ work. 

␈↓ ↓H␈↓You␈α
should␈α
convince␈α∞yourself␈α
that␈α
␈↓αmapfirst[CAR;l]␈↓␈α
will␈α∞compute␈α
␈↓αcarfirst[l]␈↓;␈α
that␈α∞exercise␈α
requires
␈↓ ↓H␈↓examining the details of ␈↓αeval␈↓.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 63␈↓␈α∂It␈α∂would␈α⊂be␈α∂better␈α∂to␈α∂call␈α⊂these␈α∂constructs␈α∂"procedure␈α∂values"␈α⊂since␈α∂we␈α∂will␈α∂take␈α⊂a␈α∂decidedly
␈↓ ↓H␈↓algorithmic␈α⊃interpretation␈α⊃of␈α∩them.␈α⊃As␈α⊃we␈α⊃now␈α∩know,␈α⊃there␈α⊃are␈α⊃important␈α∩differences␈α⊃between
␈↓ ↓H␈↓functions and algorithms.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     121␈↓


␈↓ ↓H␈↓Before␈α∞going␈α∞on␈α
to␈α∞more␈α∞complex␈α
examples␈α∞it␈α∞would␈α∞be␈α
well␈α∞to␈α∞note␈α
that␈α∞␈↓αmapfirst␈↓␈α∞is␈α∞a␈α
different
␈↓ ↓H␈↓kind␈α
of␈αLISP␈α
function␈αfrom␈α
those␈αwe␈α
have␈α
seen␈αbefore.␈α
 The␈αfirst␈α
argument␈αto␈α
␈↓αmapfirst␈↓␈αis␈α
expected
␈↓ ↓H␈↓to␈α⊃be␈α⊂a␈α⊃function.␈α⊂Notice␈α⊃that␈α⊃the␈α⊂argument␈α⊃␈↓αfn␈↓␈α⊂appears␈α⊃in␈α⊃the␈α⊂body␈α⊃of␈α⊂␈↓αmapfirst␈↓␈α⊃in␈α⊃a␈α⊂position
␈↓ ↓H␈↓reserved␈α
for␈α
functions.␈α
Thus␈α
any␈α
actual␈α
parameter␈α
bound␈α
to␈α
␈↓αfn␈↓␈α
is␈α
expected␈α
to␈α
be␈α
a␈αrepresentation␈α
of
␈↓ ↓H␈↓a function. Such a use of a function is called a ␈↓↓functional argument␈↓.

␈↓ ↓H␈↓The␈α∞trick␈α∞we␈α
used␈α∞above␈α∞of␈α
representing␈α∞the␈α∞functional␈α
argument␈α∞␈↓αcar␈↓␈α∞as␈α
a␈α∞constant␈α∞␈↓αCAR␈↓␈α∞can␈α
be
␈↓ ↓H␈↓applied to other instances of functional arguments.

␈↓ ↓H␈↓Thus the functional argument:

␈↓ ↓H␈↓␈↓ ∧i␈↓αλ[[x]f[g[x]]␈↓ could be represented as, 

␈↓ ↓H␈↓␈↓ ¬-␈↓α(LAMBDA(X)(F(G X))).␈↓ 

␈↓ ↓H␈↓The␈αdifficulty␈αis␈αthat␈αthe␈αtrick␈↓π 64␈↓␈αis␈αnot␈αsufficient␈αto␈αcapture␈αthe␈αintended␈αmeaning␈αin␈αall␈αcases.␈α To
␈↓ ↓H␈↓understand␈α∂why␈α∂␈↓αQUOTE␈↓-ing␈α∂is␈α∂not␈α∂sufficient␈α∂we␈α∂need␈α∂a␈α∂slightly␈α∂more␈α∂complex␈α∂set␈α⊂of␈α∂examples.
␈↓ ↓H␈↓First we try:

␈↓ ↓H␈↓␈↓ ↓u␈↓αmapfirst[quote[λ[[x]concat[;()]]];(A B C D)]␈↓␈↓π 65␈↓ which we expect to evaluate to ␈↓α((A)(B)(C)(D)).␈↓ 

␈↓ ↓H␈↓ ␈↓αmapfirst[quote[λ[[x]concat[x;()]]]; ..]␈↓ εh[null[l] ...]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ εh␈↓ π_E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ| /␈↓ ∧x␈↓ εh␈↓ π_| E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=>␈↓ εh______␈↓ 	λ=>  . . .
␈↓ ↓H␈↓␈↓ αλ␈↓αmapfirst␈↓ βλ| λ[[fn;l][null[l]...]]␈↓ ∧x␈↓ εhl␈↓ π_| (A  B  C D)
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλ␈↓ ∧x␈↓ εhfn␈↓ π_| quote[λ[[x]concat[x;()]]]␈↓


␈↓ ↓H␈↓Now since ␈↓αnull[l]␈↓ is false, we␈↓π 66␈↓ reduce the problem to:

␈↓ ↓H␈↓␈↓ ∧H␈↓αconcat[fn[first[l]];mapfirst[fn;rest[l]]].␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬HE␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬H| E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ ∧H______________
␈↓ ↓H␈↓␈↓ ∧H     ␈↓αl␈↓ ¬H| (A  B  C D)
␈↓ ↓H␈↓α␈↓ ∧H     fn␈↓ ¬H|quote[λ[[x]concat[x;()]]]␈↓


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 64␈↓␈α
The␈α
trick␈α
is␈α∞called␈α
␈↓αQUOTE␈↓-ing␈α
the␈α
functional␈α
argument␈α∞since␈α
the␈α
S-expr␈α
representation␈α∞of␈α
an
␈↓ ↓H␈↓instance of such a construct is a ␈↓αQUOTE␈↓-ed expression.

␈↓ ↓H␈↓␈↓π 65␈↓ Note that we are continuing to use ␈↓αquote␈↓ rather than write out the representation.

␈↓ ↓H␈↓␈↓π 66␈↓ When we say "we" we really mean ␈↓αeval␈↓.
␈↓ ↓H␈↓␈↓↓122  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓Since␈α∀we're␈α∃doing␈α∀call-by-value␈α∃we␈α∀have␈α∃to␈α∀evaluate␈α∃the␈α∀arguments␈α∃to␈α∀␈↓αconcat␈↓;␈α∃that␈α∀requires
␈↓ ↓H␈↓evaluating␈α␈↓αfn[first[l]]␈↓.␈αThe␈αvalue␈αof␈α␈↓αl␈↓␈αwe␈αfind␈α
locally␈αand␈αevaluate␈α␈↓αfirst[l]␈↓,␈αgetting␈α␈↓αA␈↓.␈αThe␈αvalue␈α
for
␈↓ ↓H␈↓␈↓αfn␈↓␈α⊃is␈α⊂also␈α⊃found␈α⊃locally,␈α⊂and␈α⊃since␈α⊂it␈α⊃is␈α⊃the␈α⊂representation␈α⊃of␈α⊂a␈α⊃λ-definition,␈α⊃we␈α⊂set␈α⊃up␈α⊃a␈α⊂new
␈↓ ↓H␈↓environment in which to evaluate the body of ␈↓αfn␈↓, binding the λ-variable ␈↓αx␈↓ to ␈↓αA␈↓:

␈↓ ↓H␈↓␈↓ ∧H␈↓αconcat[x;()]␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬HE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬H| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ∧H______________
␈↓ ↓H␈↓␈↓ ∧H      ␈↓αx␈↓ ¬H|  A␈↓


␈↓ ↓H␈↓The␈αexpected␈α
evaluation␈αtakes␈αplace:␈α
␈↓α(A)␈↓␈αis␈αcomputed␈α
and␈αreturned␈αto␈α
environment␈αE␈↓β1␈↓␈αso␈α
that␈αwe
␈↓ ↓H␈↓may continue the evaluation ␈↓αmapfirst[fn;rest[l]]␈↓.

␈↓ ↓H␈↓However, consider the following variant of this last example.  Define:

␈↓ ↓H␈↓␈↓ βi␈↓αfoo <= λ[[l]mapfirst[quote[λ[[x]concat[x;l]]]; (A B C D)]␈↓. 

␈↓ ↓H␈↓It␈α
would␈α
seem␈α
that␈α␈↓αfoo[( )]␈↓␈α
would␈α
also␈α
give␈α␈↓α((A)(B)(C)(D))␈↓␈α
since␈α
␈↓αl␈↓␈α
will␈αbe␈α
bound␈α
to␈α
␈↓α( )␈↓␈αand␈α
therefore
␈↓ ↓H␈↓the ␈↓αl␈↓ in the functional argument will effectively be ␈↓α( )␈↓.

␈↓ ↓H␈↓␈↓ ↓H     ␈↓αfoo[( )]␈↓ αH        mapfirst[quote[λ[[x]concat[x;l]]]; ..]␈↓ π(  [null[l] ...  ]
␈↓ ↓H␈↓α␈↓ ↓H␈↓ αH␈↓E␈↓β0␈↓␈↓ ∧8␈↓ ¬λ␈↓ ¬8E␈↓β1␈↓␈↓ π(␈↓ πx␈↓ λ(E␈↓β2␈↓
␈↓ ↓H␈↓␈↓ ↓H␈↓ αH | /␈↓ ∧8␈↓ ¬λ␈↓ ¬8| E␈↓β0␈↓␈↓ π(␈↓ πx␈↓ λ(| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ↓H______________␈↓ ∧8=>␈↓ ¬λ______␈↓ π(=>␈↓ πx______     =>  ...
␈↓ ↓H␈↓␈↓ ↓H     ␈↓αfoo␈↓ αH| λ[[l]...␈↓ ∧8␈↓ ¬λl␈↓ ¬8| ( )␈↓ π(␈↓ πxl␈↓ λ(| (A  B  C D)
␈↓ ↓H␈↓α␈↓ ↓Hmapfirst␈↓ αH| λ[[fn;l][null[l]...]]␈↓ ∧8␈↓ ¬λ␈↓ ¬8␈↓ π(␈↓ πxfn␈↓ λ(| quote[λ[[x]concat[x;l]]]␈↓


␈↓ ↓H␈↓␈↓αnull[l]␈↓␈αis␈αfalse␈αsince␈α␈↓αl␈↓␈αis␈α␈↓α(A␈αB␈αC␈αD)␈↓,␈αso␈αwe␈αevaluate␈α␈↓αconcat[fn[first[l]] ... ]␈↓.␈αThis␈α
involves␈αevaluating
␈↓ ↓H␈↓␈↓αfirst[l]␈↓␈α∩in␈α∩E␈↓β2␈↓,␈α∩giving␈α∩␈↓αA␈↓.␈α∩We␈α∩evaluate␈α∩(look␈α∩up)␈α∩␈↓αfn␈↓␈α∩in␈α∩E␈↓β2␈↓␈α∩and,␈α∩finding␈α∩a␈α∩representation␈α∩of␈α∩a
␈↓ ↓H␈↓λ-definition,␈αwe␈αmake␈αa␈αnew␈αenvironment␈αE␈↓β3␈↓␈αin␈αwhich␈αto␈αevaluate␈αthe␈αbody␈αof␈α␈↓αfn␈↓.␈αAs␈αwe␈αmake␈αE␈↓β3␈↓,
␈↓ ↓H␈↓we add an entry binding ␈↓αx␈↓ to ␈↓αA␈↓ and we settle down in E␈↓β3␈↓ to evaluate ␈↓αconcat[x;l]␈↓:

␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_␈↓αconcat[x;l]␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_E␈↓β3␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_| E␈↓β2␈↓
␈↓ ↓H␈↓␈↓ ∧H__________
␈↓ ↓H␈↓␈↓ ∧H ␈↓α  x␈↓ ¬_| A

␈↓ ↓H␈↓Since␈α␈↓αl␈↓␈αis␈αnon-local␈αto␈αE␈↓β3␈↓,␈αwe␈αfollow␈αthe␈αaccess␈αchain␈αto␈αfind␈αits␈αvalue␈αin␈αE␈↓β2␈↓␈αto␈αbe␈α␈↓α(A␈αB␈αC␈αD)␈↓.␈αBut
␈↓ ↓H␈↓that's not the expected value! We expected to find ␈↓α( )␈↓, which was hidden away in E␈↓β1␈↓.

␈↓ ↓H␈↓The␈α
trouble␈α
here␈αis␈α
that␈α
␈↓αl␈↓␈αwas␈α
rebound␈α
in␈αthe␈α
interim.␈α
The␈α
first␈αthing␈α
to␈α
note␈αis␈α
that␈α
the␈αproblem␈α
is
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     123␈↓


␈↓ ↓H␈↓caused␈α
by␈α
free␈α
variables:␈α
␈↓αl␈↓␈α
is␈α
free␈αin␈α
the␈α
functional␈α
argument.␈α
 Local␈α
variables␈α
aren't␈αproblematic;
␈↓ ↓H␈↓neither␈αare␈αglobal␈αvariables.␈α Next␈α
note␈αthat␈αthe␈αdesired␈αbinding␈αfor␈α
␈↓αl␈↓␈αis␈αthe␈αone␈αwhich␈αwas␈α
current
␈↓ ↓H␈↓when␈α
we␈αwere␈α
binding␈α
the␈αfunctional␈α
argument␈α
to␈αthe␈α
formal␈α
parameter␈α␈↓αfn␈↓.␈α
 A␈α
plausible␈αsolution
␈↓ ↓H␈↓then␈α
is␈αto␈α
replace␈αall␈α
non-local␈αvariables␈α
with␈αtheir␈α
values␈αat␈α
the␈αtime␈α
we␈αrecognize␈α
the␈αfunctional
␈↓ ↓H␈↓argument.␈α∂This␈α∞will␈α∂not␈α∂always␈α∞suffice.␈α∂ See␈α∞page 127␈α∂for␈α∂a␈α∞counterexample.␈α∂ A␈α∂more␈α∞promising
␈↓ ↓H␈↓solution␈αis␈αto␈αassociate␈αthe␈αname␈αof␈αthe␈αcurrent␈αenvironment␈αwith␈αthe␈αfunction␈αand␈αuse␈αthat␈αpair␈αas
␈↓ ↓H␈↓the␈αvalue␈α
to␈αbe␈α
given␈αto␈αthe␈α
formal␈αparameter.␈α
When␈αwe␈αwant␈α
to␈αapply␈α
the␈αfunctional␈αargument␈α
we
␈↓ ↓H␈↓set␈αup␈αa␈αnew␈αenvironment␈αas␈αalways,␈αintroducing␈αa␈αlocal␈αtable␈αwith␈αthe␈αλ-variables␈αbound␈α
to␈αtheir
␈↓ ↓H␈↓values;␈α∞only␈α
␈↓↓now␈↓␈α∞we␈α
use␈α∞the␈α
saved␈α∞environment␈α
as␈α∞the␈α
beginning␈α∞of␈α
the␈α∞access␈α
chain.␈α∞ Then␈α
the
␈↓ ↓H␈↓values␈α∂of␈α∂any␈α∂non-local␈α∂variables␈α∂which␈α∂we␈α∞encounter␈α∂in␈α∂the␈α∂process␈α∂of␈α∂applying␈α∂the␈α∞functional
␈↓ ↓H␈↓argument will be searched for in the saved environment.

␈↓ ↓H␈↓To␈α⊃initialize␈α⊃this␈α⊃process␈α⊃we␈α⊃require␈α⊃the␈α⊃recognition␈α⊃of␈α⊃instances␈α⊃of␈α⊃functional␈α⊃arguments.␈α⊂We
␈↓ ↓H␈↓introduce␈αa␈α
new␈αoperator␈α
called␈α␈↓αfunction␈↓.␈αThis␈α
operator␈αtakes␈α
one␈αargument:␈α
a␈αrepresentation␈αof␈α
the
␈↓ ↓H␈↓function.␈α∞The␈α∞effect␈α∞of␈α∞␈↓αfunction␈↓␈α∞will␈α∞be␈α∞to␈α∞construct␈α∞a␈α∞value␈α∞representing␈α∞that␈α∞argument␈α∞and␈α∞the
␈↓ ↓H␈↓environment which was current when the ␈↓αfunction␈↓-instance was evaluated.

␈↓ ↓H␈↓Thus␈αin␈αthe␈αexample␈αwe␈αwould␈αrecognize␈αthe␈α␈↓αfunction␈↓-construct␈αwhile␈αevaluating␈αthe␈αarguments␈αto
␈↓ ↓H␈↓␈↓αmapfirst␈↓;␈α
the␈α
environment␈α
which␈α
was␈α
current␈α
then␈α
was␈αE␈↓β1␈↓.␈α
Therefore␈α
as␈α
we␈α
build␈α
E␈↓β2␈↓␈α
we␈α
want␈αto
␈↓ ↓H␈↓associate␈α∞the␈α∞pair␈α∞␈↓αλ[[x]concat[x;l]] - ␈↓E␈↓β1␈↓␈α∞with␈α∞the␈α∞formal␈α∞parameter␈α∞␈↓αfn␈↓.␈α∞Whenever␈α∞we␈α∞apply␈α∞␈↓αfn␈↓␈α∞we
␈↓ ↓H␈↓want␈α
to␈α
use␈α
␈↓αλ[[x]concat[x;l]]␈↓;␈α
and␈α
within␈α
that␈α
context,␈α
whenever␈α
we␈α
want␈α
␈↓αl␈↓,␈α
we␈α
want␈α
the␈α
value␈α
of␈α
␈↓αl␈↓␈α
in
␈↓ ↓H␈↓E␈↓β1␈↓.

␈↓ ↓H␈↓The␈α
function-environment␈α
pair␈α
is␈α∞called␈α
a␈α
␈↓↓closure␈↓␈α
or␈α
␈↓↓funarg␈↓.␈α∞ In␈α
our␈α
diagrams␈α
we␈α∞will␈α
designate
␈↓ ↓H␈↓the pair as:

␈↓ ↓H␈↓␈↓ ¬#<function>:<environment>. 

␈↓ ↓H␈↓Thus in the above example we should designate the value of the functional argument as:

␈↓ ↓H␈↓␈↓ ¬U␈↓αλ[[x]concat[x;l]]␈↓:E␈↓β1␈↓. 

␈↓ ↓H␈↓We␈αmust␈αalso␈αextend␈αthe␈αmanipulation␈αof␈αWeizenbaum␈αenvironments␈αto␈αhandle␈αsuch␈αconstructions.
␈↓ ↓H␈↓The␈α⊂process␈α∂which␈α⊂recognizes␈α∂λ-definitions␈α⊂and␈α∂sets␈α⊂up␈α∂new␈α⊂environments␈α∂must␈α⊂now␈α⊂watch␈α∂for
␈↓ ↓H␈↓funargs.␈α
When␈α
it␈α
sees␈α
one␈αit␈α
uses␈α
the␈α
associated␈α
environment␈αas␈α
the␈α
access␈α
environment.␈α
 Let's␈αdo
␈↓ ↓H␈↓the example again.

␈↓ ↓H␈↓␈↓ αλ     ␈↓αfoo[( )]␈↓ βλ       mapfirst[function[λ[[x]concat[x;l]]; ..]␈↓ πh   [null[l] ...  ]
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλE␈↓β0␈↓α␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓α␈↓ πh␈↓ λ8␈↓ λhE␈↓β2␈↓α
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλ| /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓α␈↓ πh␈↓ λ8␈↓ λh| E␈↓β1␈↓α
␈↓ ↓H␈↓α␈↓ αλ______________␈↓ ∧x=>␈↓ ¬H______␈↓ πh=>␈↓ λ8______     => . . .
␈↓ ↓H␈↓α␈↓ αλ     foo␈↓ βλ| λ[[l]...␈↓ ∧x␈↓ ¬Hl␈↓ ¬x| ( )␈↓ πh␈↓ λ8l␈↓ λh| (A  B  C D)
␈↓ ↓H␈↓α␈↓ αλmapfirst␈↓ βλ| λ[[fn;l][null[l]...]]␈↓ ∧x␈↓ ¬H␈↓ ¬x␈↓ πh␈↓ λ8fn␈↓ λh| λ[[x]concat[x;l]]␈↓:E␈↓β1␈↓
␈↓ ↓H␈↓␈↓↓124  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓Things␈αare␈αas␈αbefore␈αexcept␈αnow␈α␈↓αfn␈↓␈αis␈αbound␈αto␈αthe␈αfunarg␈αpair␈αin␈αE␈↓β2␈↓.␈α We␈αlook␈αup␈α␈↓αfn␈↓␈αin␈αE␈↓β2␈↓␈αand,
␈↓ ↓H␈↓finding␈αa␈αλ-definition,␈αwe␈αmake␈αa␈αnew␈αenvironment␈αE␈↓β3␈↓␈αin␈αwhich␈αto␈αevaluate␈αthe␈αbody␈αof␈α␈↓αfn␈↓.␈αAs␈α
we
␈↓ ↓H␈↓make␈αE␈↓β3␈↓,␈αwe␈αadd␈α
an␈αentry␈αbinding␈α␈↓αx␈↓␈α
to␈α␈↓αA␈↓.␈α But␈αnow␈α
since␈αthe␈αλ-definition␈αis␈α
a␈αfunarg␈αwe␈αmake␈α
the
␈↓ ↓H␈↓access environment E␈↓β1␈↓ as saved with ␈↓αfn␈↓.  Thus we settle down in E␈↓β3␈↓ to evaluate ␈↓αconcat[x;l]␈↓:

␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_␈↓αconcat[x;l]␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_E␈↓β3␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ∧H__________
␈↓ ↓H␈↓␈↓ ∧H ␈↓α  x␈↓ ¬_| A

␈↓ ↓H␈↓Since␈α
␈↓αl␈↓␈α
is␈α
non-local␈α
to␈α∞E␈↓β3␈↓,␈α
we␈α
follow␈α
the␈α
access␈α∞chain␈α
to␈α
find␈α
its␈α
value␈α∞in␈α
E␈↓β1␈↓␈α
to␈α
be␈α
␈↓α( )␈↓␈α∞as␈α
desired.
␈↓ ↓H␈↓Thus instead of simply tracing back to the previous environment we detour around E␈↓β2␈↓:

␈↓"␈↓ ↓H␈↓∂␈↓ ¬_␈↓E␈↓β0␈↓∂
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_↑
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_~
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_␈↓E␈↓β1␈↓∂←␈↓ ¬H⊃
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_↑␈↓ ¬H.
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_~␈↓ ¬H.
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_␈↓E␈↓β2␈↓∂␈↓ ¬H↑
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_↑␈↓ ¬H.
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_~␈↓ ¬H.
␈↓"␈↓ ↓H␈↓∂␈↓ ¬_␈↓E␈↓β3␈↓∂→␈↓ ¬H$

␈↓ ↓H␈↓However,␈α∩there␈α∪is␈α∩still␈α∪some␈α∩information␈α∪which␈α∩we␈α∪must␈α∩make␈α∪explicit␈α∩if␈α∪these␈α∩Weizenbaum
␈↓ ↓H␈↓diagrams␈αare␈αto␈αfaithfully␈αrepresent␈αthe␈αprocess␈αof␈αevaluation.␈α Namely,␈αafter␈αwe␈αhave␈αfinished␈αthe
␈↓ ↓H␈↓evaluation␈αof␈α␈↓αconcat[x;l]␈↓␈αwe␈αare␈αto␈αrestore␈αa␈αprevious␈αenvironment.␈αWhich␈αone␈αis␈αit?␈α It␈αisn't␈αE␈↓β1␈↓,␈αit's
␈↓ ↓H␈↓E␈↓β2␈↓! That information is not available in our diagram, so we must correct the situation.

␈↓ ↓H␈↓In␈α
the␈α
left-hand␈α
quadrant␈α
of␈α
our␈α
diagram␈α
we␈α
place␈α
the␈α
name␈α
of␈α
the␈α
environment␈α
which␈α
we␈αwish
␈↓ ↓H␈↓restored␈α
when␈α
we␈αleave␈α
the␈α
current␈αenvironment.␈α
That␈α
environment␈α
name␈αwill␈α
be␈α
called␈αthe␈α
␈↓↓control
␈↓ ↓H␈↓↓environment␈↓,␈α⊂and␈α⊂will␈α⊃head␈α⊂a␈α⊂chain␈α⊃of␈α⊂environments,␈α⊂called␈α⊂the␈α⊃control␈α⊂chain␈α⊂␈↓π 67␈↓.␈α⊃ Here's␈α⊂the
␈↓ ↓H␈↓correct picture:

␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_␈↓αconcat[x;l]␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬_E␈↓β3␈↓
␈↓ ↓H␈↓␈↓ ∧HE␈↓β2␈↓␈↓ ¬_| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ∧H__________
␈↓ ↓H␈↓␈↓ ∧H ␈↓α  x␈↓ ¬_| A


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 67␈↓␈αIn␈αAlgol,␈αthe␈αaccess␈αchain␈αis␈αcalled␈αthe␈αstatic␈αchain,␈αand␈αthe␈αcontrol␈αchain␈αis␈αcalled␈αthe␈αdynamic
␈↓ ↓H␈↓chain.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     125␈↓


␈↓ ↓H␈↓So␈α⊃after␈α⊃we␈α⊂have␈α⊃finished␈α⊃the␈α⊂computation␈α⊃in␈α⊃E␈↓β3␈↓␈α⊂we␈α⊃return␈α⊃control␈α⊂to␈α⊃E␈↓β2␈↓.␈α⊃ Thus␈α⊃the␈α⊂general
␈↓ ↓H␈↓structure of an environment is as follows:

␈↓ ↓H␈↓␈↓ ¬H␈↓ εX␈↓	Form␈↓
␈↓ ↓H␈↓␈↓ ¬H␈↓ εXE␈↓βcurrent␈↓
␈↓ ↓H␈↓␈↓ ¬HE␈↓βcontrol␈↓␈↓ εX| E␈↓βaccess␈↓
␈↓ ↓H␈↓␈↓ ¬H______________________________
␈↓ ↓H␈↓␈↓ ¬Hvar␈↓ εX| value
␈↓ ↓H␈↓␈↓ ¬H␈↓αx␈↓β1␈↓␈↓ εX| . . .
␈↓ ↓H␈↓␈↓ ¬H␈↓αx␈↓β2␈↓␈↓ εX| . . .
␈↓ ↓H␈↓␈↓ ¬H. . .␈↓ εX| . . .
␈↓ ↓H␈↓␈↓ ¬H␈↓αx␈↓βn␈↓␈↓ εX| . . .
␈↓ ↓H␈↓␈↓ ¬H␈↓ εX|


␈↓ ↓H␈↓Here's␈α
another␈α
example,␈α
involving␈α
a␈α
function␈α
to␈α
produce␈α
the␈α
composition␈α
of␈α
two␈α
unary␈αfunctions.
␈↓ ↓H␈↓We␈αwill␈αcall␈αthe␈αfunction␈α␈↓αcompose␈↓.␈αThe␈αvalue␈αreturned␈αby␈α␈↓αcompose␈↓␈αwill␈αbe␈αa␈αfunction.␈αThus␈α
␈↓αcompose␈↓
␈↓ ↓H␈↓will produce functional values:

␈↓ ↓H␈↓α␈↓ ∧>compose[function[car];function[cdr]] = cadr
␈↓ ↓H␈↓α␈↓with a plausible definition as:␈↓α
␈↓ ↓H␈↓α␈↓ ¬λcompose <= λ[[f;g]λ[[x]f[g[x]]]]


␈↓ ↓H␈↓This␈α∂definition␈α∂of␈α∂␈↓αcompose␈↓␈α∂is␈α∞almost␈α∂right.␈α∂ The␈α∂value␈α∂returned␈α∞by␈α∂␈↓αcompose␈↓␈α∂is␈α∂to␈α∂be␈α∂a␈α∞function.
␈↓ ↓H␈↓Indeed␈α∂it␈α∂is␈α∂an␈α∞instance␈α∂of␈α∂a␈α∂␈↓↓functional␈α∞value␈↓,␈α∂so,␈α∂as␈α∂with␈α∞functional␈α∂arguments,␈α∂it␈α∂needs␈α∂to␈α∞be
␈↓ ↓H␈↓decorated␈αwith␈α
␈↓αfunction␈↓␈αso␈αthat␈α
the␈αenvironment␈α
which␈αcontains␈αthe␈α
right␈αbindings␈αfor␈α
␈↓αf␈↓␈αand␈α
␈↓αg␈↓␈αis
␈↓ ↓H␈↓saved.␈α⊗Which␈α⊗environment␈α⊗is␈α↔that?␈α⊗It's␈α⊗the␈α⊗environment␈α↔which␈α⊗will␈α⊗be␈α⊗current␈α↔when␈α⊗the
␈↓ ↓H␈↓␈↓αfunction␈↓-construct is recognized.  So we write:

␈↓ ↓H␈↓␈↓ εP␈↓ ∧G␈↓αcompose <= λ[[f;g]function[λ[[x]f[g[x]]]]].

␈↓ ↓H␈↓α␈↓ β*␈↓Now try: ␈↓αapp[cons[A;(B . C)];compose[function[car];function[cdr]]]␈↓
␈↓ ↓H␈↓␈↓ ¬*where: ␈↓αapp <= λ[[y;f]f[y]]␈↓

␈↓ ↓H␈↓As␈αusual␈αwe␈α
evaluate␈αthe␈αarguments␈α
to␈α␈↓αapp␈↓,␈αbind␈α
the␈αresults␈αto␈α
␈↓αy␈↓␈αand␈α␈↓αf␈↓␈α
and␈αevaluate␈αthe␈α
body␈αof
␈↓ ↓H␈↓␈↓αapp␈↓.

␈↓ ↓H␈↓␈↓ β(␈↓αapp[cons[A;(B .C)];function[compose[function[car];function[cdr]]]]␈↓
␈↓ ↓H␈↓␈↓ β(␈↓ ∧(E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ β(␈↓ ∧(/| /
␈↓ ↓H␈↓␈↓ β(______________
␈↓ ↓H␈↓␈↓ β(     ␈↓αapp␈↓ ∧( | λ[[y;f]f[y]]
␈↓ ↓H␈↓α␈↓ β(compose␈↓ ∧( | λ[[f;g]function[λ[[x]f[g[x]]]]]␈↓
␈↓ ↓H␈↓␈↓↓126  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓Evaluation␈α∪of␈α∪the␈α∀first␈α∪argument␈α∪to␈α∀␈↓αapp␈↓␈α∪brings␈α∪no␈α∪surprises;␈α∀we␈α∪get␈α∪␈↓α(A .(B . C))␈↓.␈α∀We␈α∪begin
␈↓ ↓H␈↓evaluating␈αthe␈αsecond␈αargument;␈αwe␈αfind␈αthe␈αdefinition␈αof␈α␈↓αcompose␈↓␈αin␈αthe␈αenvironment␈αand␈αsince␈αit
␈↓ ↓H␈↓is a λ-definition we set up a new environment, E␈↓β1␈↓, and evaluate the body ␈↓αfunction[λ[[x]f[g[x]]]]␈↓:

␈↓ ↓H␈↓␈↓ ∧H␈↓αfunction[λ[[x]f[g[x]]]]␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬HE␈↓β1␈↓
␈↓ ↓H␈↓␈↓ ∧H     E␈↓β0␈↓␈↓ ¬H| E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ ∧H     __________
␈↓ ↓H␈↓␈↓ ∧H       ␈↓αf␈↓ ¬H| car␈↓:E␈↓β0␈↓α
␈↓ ↓H␈↓α␈↓ ∧H       g␈↓ ¬H| cdr␈↓:E␈↓β0␈↓

␈↓ ↓H␈↓Again,␈α_the␈α↔recognition␈α_of␈α↔the␈α_␈↓αfunction␈↓-construct␈α↔says␈α_return␈α↔a␈α_funarg-pair␈α↔as␈α_value.␈α↔The
␈↓ ↓H␈↓environment␈α
we␈α
associate␈α
is␈α
the␈α
current␈α
one,␈α
E␈↓β1␈↓.␈α∞We␈α
now␈α
go␈α
back␈α
to␈α
E␈↓β0␈↓,␈α
using␈α
the␈α∞control␈α
chain.
␈↓ ↓H␈↓Since␈α
both␈αarguments␈α
to␈α␈↓αapp␈↓␈α
are␈αnow␈α
evaluated,␈αwe␈α
find␈αthe␈α
definition␈αof␈α
␈↓αapp␈↓␈αand␈α
set␈αup␈α
a␈αnew
␈↓ ↓H␈↓environment E␈↓β2␈↓.  Thus:

␈↓ ↓H␈↓␈↓ β(␈↓ ∧(␈↓αf[y]␈↓ ε_␈↓ εh␈↓ π_f[g[x]]␈↓
␈↓ ↓H␈↓␈↓ β(␈↓ ∧(E␈↓β2␈↓␈↓ ε_␈↓ εh␈↓ π_E␈↓β3␈↓
␈↓ ↓H␈↓␈↓ β(   E␈↓β0␈↓␈↓ ∧(| E␈↓β0␈↓␈↓ ε_␈↓ εh E␈↓β2␈↓␈↓ π_| E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ β(    __________␈↓ ε_=>␈↓ εh_________
␈↓ ↓H␈↓␈↓ β(     ␈↓αy␈↓ ∧(| (A. (B . C))␈↓ ε_␈↓ εhx␈↓ π_| (A. (B . C))
␈↓ ↓H␈↓α␈↓ β(     f␈↓ ∧(| λ[[x]f[g[x]]]␈↓:E␈↓β1␈↓

␈↓ ↓H␈↓The␈αform␈αto␈αbe␈αevaluated␈αin␈αE␈↓β2␈↓␈αis␈α␈↓αf[y]␈↓;␈αwe␈αfind␈α␈↓αy␈↓␈αand␈α␈↓αf␈↓␈αboth␈αlocally.␈αWe␈αevaluate␈αthe␈αargument␈α␈↓αy␈↓,
␈↓ ↓H␈↓then␈αsince␈α
␈↓αf␈↓␈αis␈αa␈α
λ-definition,␈αwe␈αset␈α
up␈αa␈αnew␈α
environment␈αbinding␈αthe␈α
λ-variable␈α␈↓αx␈↓␈αto␈α
the␈αvalue
␈↓ ↓H␈↓␈↓α(A .(B . C))␈↓.␈αBut␈αthe␈αλ-definition␈αis␈αalso␈αa␈αfunarg;␈αtherefore␈αthe␈αaccess␈αenvironment␈αstored␈αin␈αE␈↓β3␈↓␈αis
␈↓ ↓H␈↓E␈↓β1␈↓.␈α The␈αcontrol␈αcomponent␈αof␈αE␈↓β3␈↓␈αis␈αset␈αto␈αthe␈αprior␈αenvironment,␈αE␈↓β2␈↓;␈αand␈αwe␈αbegin␈αevaluation␈αof
␈↓ ↓H␈↓the body ␈↓αf[g[x]]␈↓.

␈↓ ↓H␈↓Now␈αin␈αE␈↓β3␈↓␈αwe␈αfind␈α␈↓αx␈↓␈αlocally␈αbut␈αhave␈αto␈αresort␈αto␈αthe␈αaccess␈αchain␈αto␈αfind␈α␈↓αf␈↓␈αand␈α␈↓αg␈↓;␈αusing␈αfunargs,
␈↓ ↓H␈↓we have set up the appropriate environments. From E␈↓β3␈↓ we have access to E␈↓β1␈↓␈↓π 68␈↓:
␈↓"␈↓ ↓H␈↓∂                            ␈↓E␈↓β0␈↓∂
␈↓"␈↓ ↓H␈↓∂                            /\
␈↓"␈↓ ↓H␈↓∂                           /  \
␈↓"␈↓ ↓H␈↓∂                          /    \
␈↓"␈↓ ↓H␈↓∂                         ␈↓E␈↓β1␈↓∂      ␈↓E␈↓β2␈↓∂
␈↓"␈↓ ↓H␈↓∂                           .     \
␈↓"␈↓ ↓H␈↓∂                              .   \
␈↓"␈↓ ↓H␈↓∂                                 .␈↓E␈↓β3␈↓∂

␈↓ ↓H␈↓The␈α
rest␈α
of␈αthe␈α
evaluation␈α
goes␈αwithout␈α
a␈α
hitch:␈αwe␈α
finish␈α
the␈αevaluation␈α
in␈α
E␈↓β3␈↓␈αand␈α
return␈α
to␈αE␈↓β2␈↓
␈↓ ↓H␈↓and finally to E␈↓β0␈↓ following the control evironments.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 68␈↓ and from there to E␈↓β0␈↓ if it were needed.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     127␈↓


␈↓ ↓H␈↓Notice␈αthat␈α␈↓αf␈↓␈αand␈α␈↓αg␈↓␈αin␈αthe␈αbody␈αof␈α␈↓αcompose␈↓␈αare␈αfree␈αvariables␈αand␈αtherefore␈αtheir␈αbindings␈αare␈αnot
␈↓ ↓H␈↓to␈αbe␈α
found␈αin␈α
the␈αlocal␈α
environment.␈α Since␈αthe␈α
interesting␈αapplications␈α
of␈αsuch␈α
functions␈αusually
␈↓ ↓H␈↓involve␈α⊂free␈α⊂variables,␈α⊂we␈α⊂must␈α⊂deal␈α⊃with␈α⊂them.␈α⊂ In␈α⊂particular,␈α⊂the␈α⊂␈↓αlabel␈↓␈α⊂operator␈α⊃will␈α⊂typically
␈↓ ↓H␈↓involve free variables. We remarked that ␈↓αf␈↓ in:
␈↓ ↓H␈↓␈↓ ∧O␈↓αf <= λ[[x][zerop[x] → 1; ␈↓
t␈↓α → *[x;f[x-1]]] ]␈↓

␈↓ ↓H␈↓is␈αfree.␈α
But␈αwe␈α
want␈αthe␈α
occurrence␈αof␈α
␈↓αf␈↓␈αon␈αthe␈α
right␈αto␈α
be␈αsynonymous␈α
with␈αthe␈α
␈↓αf␈↓␈αbeing␈αdefined␈α
on
␈↓ ↓H␈↓the␈αleft.␈αWe␈αcan␈α
do␈αthis␈αby␈α"tying␈αa␈α
knot"␈αin␈αthe␈αaccess␈α
environment␈αchain.␈αThus␈αwe␈αshould␈α
modify
␈↓ ↓H␈↓the diagram for ␈↓αlabel␈↓ to be:

␈↓ ↓H␈↓␈↓ αλ␈↓αlabel[f;λ[[x]␈↓λx␈↓α[x]]][A]␈↓␈↓ ∧x␈↓ ¬H␈↓λx␈↓α[x]␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλE␈↓β0␈↓␈↓ ∧x␈↓ ¬H␈↓ ¬xE␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ | /␈↓ ∧x␈↓ ¬H␈↓ ¬x| E␈↓β0␈↓
␈↓ ↓H␈↓␈↓ αλ______________␈↓ ∧x=> ...␈↓ ¬H______
␈↓ ↓H␈↓␈↓ αλ␈↓ βλ |␈↓ ∧x␈↓ ¬H␈↓αf␈↓ ¬x| λ[[x]␈↓λx␈↓α[x]]␈↓:E␈↓β1␈↓α
␈↓ ↓H␈↓α␈↓ αλ␈↓ βλ␈↓ ∧x␈↓ ¬Hx␈↓ ¬x|A

␈↓ ↓H␈↓Notice␈αthat␈α
the␈αeffect␈αof␈α
␈↓αlabel␈↓␈αis␈αto␈α
build␈α␈↓αfunction[λ[[x]␈↓λx␈↓α]]␈↓␈αand␈α
assocate␈αthat␈αwith␈α
␈↓αf␈↓.␈α If␈αwe␈α
attempted
␈↓ ↓H␈↓to␈α⊃implement␈α⊂␈↓αfunction[␈↓λf␈↓α]␈↓␈α⊃by␈α⊂replacing␈α⊃all␈α⊂non-local␈α⊃variables␈α⊂in␈α⊃␈↓λf␈↓␈α⊂by␈α⊃their␈α⊂current␈α⊃values␈α⊂we
␈↓ ↓H␈↓wouldn't always get what we expect.
␈↓ ↓H␈↓Consider ␈↓ ∧B␈↓α fact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]]␈↓:
␈↓ ↓H␈↓If the current environment is E␈↓βi␈↓:

␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αfoo␈↓

␈↓ ↓H␈↓then executing ␈↓αfact <=  ... ␈↓  should give something like:

␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓

␈↓ ↓H␈↓rather than:
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...foo[x-1]]

␈↓ ↓H␈↓Since␈α
every␈α
language␈α
construct␈α∞in␈α
LISP␈α
must␈α
have␈α
an␈α∞S-expr␈α
representation␈α
we␈α
must␈α∞include␈α
the
␈↓ ↓H␈↓␈↓αfunction␈↓-construct. Its translation scheme is simple: represent ␈↓αfunction[␈↓λx␈↓α]␈↓ as ␈↓α(FUNCTION ␈↓
R␈↓∞(␈↓λx␈↓∞)␈↓α)␈↓.
␈↓ ↓H␈↓␈↓↓128  Evaluation␈↓ &4.10␈↓



␈↓ ↓H␈↓Thus:
␈↓ ↓H␈↓␈↓ ∧C␈↓αfunction[λ[[x]f[g[x]]] ␈↓ has an ␈↓
R␈↓-image of,

␈↓ ↓H␈↓␈↓ ∧M␈↓α(FUNCTION(LAMBDA(X)(F(G X)))).␈↓

␈↓ ↓H␈↓Similarly␈α⊂we␈α⊂must␈α⊂develop␈α⊂parts␈α⊂of␈α⊂␈↓αeval␈↓␈α⊂to␈α⊂deal␈α⊂with␈α⊂␈↓αFUNCTION␈↓.␈α⊂ The␈α⊂device␈α⊂LISP␈α⊂used␈α⊂to
␈↓ ↓H␈↓associate␈α⊗environments␈α∃with␈α⊗functions␈α∃is␈α⊗called␈α⊗the␈α∃␈↓αFUNARG␈↓␈α⊗device.␈α∃(More␈α⊗is␈α⊗said␈α∃about
␈↓ ↓H␈↓implementations␈α⊃of␈α⊃␈↓αFUNARG␈↓␈α∩in␈α⊃Section .)␈α⊃When␈α⊃␈↓αeval␈↓␈α∩sees␈α⊃the␈α⊃construction␈α∩␈↓α(FUNCTION␈α⊃␈↓fn␈↓α)␈↓
␈↓ ↓H␈↓it returns as value the list:

␈↓ ↓H␈↓␈↓ ∧K␈↓α(FUNARG   ␈↓fn    current-symbol-table␈↓α)␈↓. 

␈↓ ↓H␈↓When␈α
␈↓αeval␈↓,␈α∞or␈α
actually␈α∞␈↓αapply␈↓␈α
sees␈α∞␈↓α(FUNARG␈α
␈↓fn␈α∞st␈↓α)␈↓,␈α
that␈α
is,␈α∞when␈α
we␈α∞are␈α
␈↓↓calling␈↓α␈α∞fn␈↓,␈α
we␈α∞use␈α
the
␈↓ ↓H␈↓symbol table ␈↓αst␈↓, rather than the current symbol table for accessing free variables.

␈↓ ↓H␈↓Thus␈α
there␈α
are␈α␈↓↓two␈↓␈α
environments␈α
involved␈αin␈α
the␈α
proper␈αhandling␈α
of␈α
functional␈α
arguments.␈αFirst
␈↓ ↓H␈↓there␈α∃is␈α∀the␈α∃environment␈α∃which␈α∀is␈α∃saved␈α∃with␈α∀the␈α∃␈↓αFUNARG␈↓.␈α∃ This␈α∀is␈α∃called␈α∃the␈α∀␈↓↓binding
␈↓ ↓H␈↓↓environment␈↓␈α∃since␈α∃it␈α∃is␈α∃the␈α∃environment␈α∃current␈α∃at␈α∃the␈α∃time␈α∃the␈α∃functional␈α∃argument␈α∃was
␈↓ ↓H␈↓constructed␈α⊗or␈α⊗bound.␈α↔The␈α⊗second␈α⊗environment,␈α⊗called␈α↔the␈α⊗␈↓↓activation␈α⊗environment␈↓,␈α↔is␈α⊗the
␈↓ ↓H␈↓environment␈α⊂which␈α⊂is␈α⊂current␈α∂when␈α⊂the␈α⊂functional␈α⊂argument␈α∂is␈α⊂␈↓↓applied␈↓␈α⊂or␈α⊂activated.␈α⊂Thus␈α∂the
␈↓ ↓H␈↓activation␈αenvironment␈αis␈α
used␈αto␈αlocate␈αlocal␈α
variables,␈αbut␈αif␈αa␈α
non-local␈αvariable␈αis␈α
needed␈αthen
␈↓ ↓H␈↓the binding environment is selected.

␈↓ ↓H␈↓It␈αis␈αthe␈αduty␈αof␈α␈↓αeval␈↓␈αand␈α␈↓αapply␈↓␈αto␈αuse␈αthe␈α␈↓αFUNARG␈↓␈αdevice␈αto␈αmaintain␈αthe␈αproper␈αcontrol␈αof␈αthe
␈↓ ↓H␈↓activation and binding environments.

␈↓ ↓H␈↓Finally,␈α∂we␈α∂should␈α⊂update␈α∂our␈α∂description␈α⊂of␈α∂the␈α∂usage␈α⊂of␈α∂Weizenbaum␈α∂environments␈α⊂given␈α∂on
␈↓ ↓H␈↓page 117:

␈↓ ↓H␈↓␈↓ α_When␈α
the␈α␈↓αfunction␈↓␈α
construct␈αis␈α
recognized,␈α
we␈αmanufacture␈α
a␈α␈↓αFUNARG␈↓␈α
triple␈α
consisting␈αof
␈↓ ↓H␈↓␈↓ α_the␈α⊃atom␈α⊃␈↓αFUNARG␈↓,␈α⊂the␈α⊃function␈α⊃described␈α⊂in␈α⊃the␈α⊃instance␈α⊂of␈α⊃␈↓αfunction␈↓,␈α⊃and␈α⊃the␈α⊂current
␈↓ ↓H␈↓␈↓ α_environment.␈α
 This␈α
triple␈α∞is␈α
the␈α
value␈α
of␈α∞the␈α
␈↓αfunction␈↓␈α
construct␈α
and␈α∞may␈α
thus␈α
be␈α∞bound␈α
to
␈↓ ↓H␈↓␈↓ α_any␈α∞LISP␈α∞variable;␈α∞typically␈α∂the␈α∞LISP␈α∞variable␈α∞will␈α∞appear␈α∂in␈α∞an␈α∞expression␈α∞in␈α∂a␈α∞position
␈↓ ↓H␈↓␈↓ α_reserved for functions.

␈↓ ↓H␈↓␈↓ α_When␈αdoing␈αa␈αλ-binding,␈αset␈αup␈αa␈αnew␈α
E␈↓βnew␈↓␈αwith␈αthe␈αλ-variables␈αas␈αthe␈αlocal␈αvariable␈α
entries
␈↓ ↓H␈↓␈↓ α_and␈αthe␈αvalues␈αof␈αthe␈αarguments␈αas␈αthe␈αcorresponding␈αvalue␈αentries.␈α The␈αcontrol␈αslot␈αof␈αthe
␈↓ ↓H␈↓␈↓ α_new␈α⊃E␈↓βnew␈↓␈α⊃always␈α⊃points␈α⊃to␈α⊂the␈α⊃previous␈α⊃symbol␈α⊃table.␈α⊃The␈α⊂access␈α⊃slot␈α⊃also␈α⊃points␈α⊃to␈α⊂the
␈↓ ↓H␈↓␈↓ α_previous␈αenvironment␈α
unless␈αthe␈α
function␈αbeing␈α
applied␈αis␈α
a␈α␈↓αFUNARG␈↓.␈α
 If␈αit␈α
␈↓↓is␈↓␈αa␈α
␈↓αFUNARG␈↓,
␈↓ ↓H␈↓␈↓ α_then set the access slot to the environment which was saved with the ␈↓αFUNARG␈↓.

␈↓ ↓H␈↓␈↓ α_The␈α
evaluation␈α
of␈α
the␈α
body␈αof␈α
the␈α
λ-␈α
expression␈α
takes␈αplace␈α
using␈α
E␈↓βnew␈↓;␈α
when␈α
a␈αlocal␈α
variable
␈↓ ↓H␈↓␈↓ α_is␈αaccessed␈αwe␈αfind␈αit␈αin␈αE␈↓βnew␈↓;␈αwhen␈αa␈αnon-local␈αvariable␈αoccurs,␈αwe␈αchase␈αthe␈αaccess␈αchain␈αto
␈↓ ↓H␈↓␈↓ α_find␈α
its␈αvalue.␈α
 When␈αthe␈α
evaluation␈α
of␈αthe␈α
body␈αis␈α
completed,␈αthe␈α
previous␈α
environment␈αis
␈↓ ↓H␈↓␈↓ α_restored.  E␈↓βnew␈↓ disappears unless the value of the computation is a functional value.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     129␈↓


␈↓ ↓H␈↓Notice␈αthat␈α
there␈αis␈α
a␈αcertain␈α
asymmetry␈αabout␈αaccess␈α
and␈αcontrol.␈α
 The␈αcontrol␈α
slot␈αalways␈αpoints␈α
at
␈↓ ↓H␈↓the␈α
previous␈α
environment,␈α
while␈α
the␈α
access␈α
slot␈α
may␈αvary.␈α
It␈α
may␈α
follow␈α
control,␈α
as␈α
is␈α
the␈α
case␈αon
␈↓ ↓H␈↓simple␈αfunction␈αcalls;␈αit␈αmay␈αpoint␈αto␈αan␈αenvironment␈αfurther␈αdown␈αthe␈αcontrol␈αchain,␈αas␈αis␈αthe␈α
case
␈↓ ↓H␈↓for␈αfunctional␈αarguments;␈αit␈αmay␈αpoint␈αto␈αan␈αenvironment␈αwhich␈αcontrol␈αcannot␈αreturn␈αto,␈αas␈αis␈αthe
␈↓ ↓H␈↓case for functional values; or it may point to itself as is the case for ␈↓αlabel␈↓'s implementation.

␈↓ ↓H␈↓There␈α
is␈αanother␈α
asymmetry␈α
in␈αthe␈α
properties␈αof␈α
access␈α
and␈αcontrol.␈α
 The␈αaccess␈α
environment␈α
is␈αa
␈↓ ↓H␈↓self-sufficient␈α∃data␈α∀structure;␈α∃it␈α∀can␈α∃be␈α∀described␈α∃and␈α∀manipulated␈α∃as␈α∀such␈α∃using␈α∃the␈α∀usual
␈↓ ↓H␈↓constructors,␈αselectors,␈αand␈α
recognizers.␈α Typically␈αsuch␈αenvironments␈α
come␈αinto␈αexistence␈αas␈α
a␈αpart
␈↓ ↓H␈↓of␈αa␈αcomputation;␈αthey␈α
are␈αconstructed␈αduring␈αthe␈αλ-binding␈α
process.␈α We␈αcan␈αimplicitly␈α
save␈αsuch
␈↓ ↓H␈↓an␈α⊂environment␈α⊂through␈α⊂the␈α⊂␈↓αFUNARG␈↓␈α⊂device;␈α⊂and␈α⊂we␈α⊂can␈α⊂explicitly␈α⊂build␈α⊂such␈α⊂environments
␈↓ ↓H␈↓using␈αthe␈αdata␈αstructure␈αoperations␈αand␈αpass␈αthem␈αto␈α␈↓αeval␈↓␈αas␈αa␈αsymbol␈αtable.␈αBut␈αsymbol␈αtables␈αare
␈↓ ↓H␈↓independent␈αof␈αthe␈αmethod␈αused␈αto␈αcreate␈αthem.␈αIn␈αparticular,␈αonce␈αa␈αtable␈αhas␈αbeen␈αcaptured␈αby␈αa
␈↓ ↓H␈↓␈↓αFUNARG␈↓␈α∂we␈α∂need␈α∂not␈α∞retain␈α∂any␈α∂information␈α∂about␈α∞the␈α∂computation␈α∂which␈α∂created␈α∂that␈α∞table.
␈↓ ↓H␈↓However␈αthe␈αidea␈αof␈α"control"␈αand␈α"state␈αof␈αcomputation"␈αis␈αintegrally␈αtied␈αto␈αaccess␈αstructure.␈α The
␈↓ ↓H␈↓state␈α∂of␈α∂the␈α∂computation␈α∂involves␈α∂the␈α⊂expression␈α∂currently␈α∂being␈α∂evaluated,␈α∂the␈α∂history␈α⊂of␈α∂those
␈↓ ↓H␈↓computations␈αwhich␈αare␈αsuspended␈αand␈αwaiting␈αfor␈αthe␈αcompletion␈αof␈αthe␈αcurrent␈αcomputation,␈α
and
␈↓ ↓H␈↓it␈αalso␈αinvolves␈αthe␈αaccess␈αevironment␈αsince␈αthat␈αis␈αnecessary␈αfor␈αthe␈αcrrect␈αevaluation␈αof␈αvariables.
␈↓ ↓H␈↓To␈α"save␈αthe␈αstate␈αof␈αcomputation"␈αimplies␈αsaving␈αthe␈αpartial␈αcomputation␈αto␈αthat␈αpoint,␈αsaving␈αthe
␈↓ ↓H␈↓expression being evaluated, and saving the current access environment.

␈↓ ↓H␈↓Thus␈αto␈α
a␈αlarge␈α
extent␈α"control␈α
environment"␈αis␈αa␈α
misnomer.␈αWhat␈α
we␈αare␈α
intending␈αto␈α
capture␈αis
␈↓ ↓H␈↓the␈α∀idea␈α∪of␈α∀a␈α∪suspended␈α∀computation:␈α∀suspended␈α∪until␈α∀the␈α∪subsidiary␈α∀computation␈α∀has␈α∪been
␈↓ ↓H␈↓completed.␈αPart␈αof␈αthe␈αsuspended␈αcomputation␈α
␈↓↓is␈↓␈αthe␈α"control␈αenvironment",␈αbut␈αthere's␈α
more.␈αThe
␈↓ ↓H␈↓Weizenbaum␈α∀diagrams␈α∃show␈α∀part␈α∀of␈α∃the␈α∀information;␈α∀they␈α∃show␈α∀the␈α∀environments␈α∃and␈α∀the
␈↓ ↓H␈↓expressions␈αbeing␈α
evaluated.␈αHowever␈αthey␈α
leave␈αimplicit␈αthe␈α
dynamics␈αof␈αthe␈α
computation:␈αwhich
␈↓ ↓H␈↓argument␈α⊂is␈α⊂being␈α∂evaluated,␈α⊂and␈α⊂where␈α∂are␈α⊂the␈α⊂partial␈α∂results␈α⊂being␈α⊂stored,␈α∂and␈α⊂where␈α⊂in␈α∂the
␈↓ ↓H␈↓expression␈α∞we␈α∞are␈α∞to␈α∂continue␈α∞when␈α∞the␈α∞subsidiary␈α∂computation␈α∞is␈α∞completed.␈α∞In␈α∂Section 4.15␈α∞we
␈↓ ↓H␈↓will␈α∞develop␈α∞a␈α∞different␈α∞␈↓αeval␈↓␈α∞family␈α∞which␈α∞will␈α∞make␈α∞much␈α∞of␈α∞this␈α∞information␈α∞explicit.␈α∞ Also␈α
in
␈↓ ↓H␈↓Section 4.15␈α∞we␈α∂will␈α∞examine␈α∂the␈α∞possibility␈α∂of␈α∞expanding␈α∞the␈α∂behavior␈α∞of␈α∂control␈α∞slots.␈α∂That␈α∞is,
␈↓ ↓H␈↓allowing environments other than the predecessor to appear in the control slot of an environment.

␈↓ ↓H␈↓What␈αdoes␈αall␈αthis␈αsay␈αabout␈αfunctions?␈α We␈αhave␈αalready␈αremarked␈αthat␈αfunctions␈αare␈αparametric
␈↓ ↓H␈↓values;␈α∞to␈α∞that␈α∞we␈α∞must␈α∞add␈α∞that␈α∞functions␈α∂are␈α∞also␈α∞tied␈α∞to␈α∞the␈α∞environment␈α∞in␈α∞which␈α∂they␈α∞were
␈↓ ↓H␈↓created;␈αthey␈α
cannot␈αbe␈αevaluated␈α
␈↓αin␈αvacuo␈↓.␈α What␈α
does␈αthis␈αsay␈α
about␈α"<="?␈αIt␈α
␈↓↓still␈↓␈αappears␈α
to␈αact
␈↓ ↓H␈↓like␈α∪an␈α∪assignment␈α∩statement.␈α∪ It␈α∪is␈α∪taking␈α∩on␈α∪more␈α∪distinct␈α∩character␈α∪since␈α∪it␈α∪must␈α∩associate
␈↓ ↓H␈↓environments with the function body as it does the assignment.

␈↓ ↓H␈↓The␈α⊃correct␈α∩implementation␈α⊃of␈α∩the␈α⊃semantics␈α∩of␈α⊃␈↓αfunction␈↓␈α⊃seems␈α∩like␈α⊃a␈α∩lot␈α⊃of␈α∩work␈α⊃to␈α∩allow␈α⊃a
␈↓ ↓H␈↓moderately␈α⊗obscure␈α⊗construct␈α⊗to␈α⊗appear␈α⊗in␈α⊗a␈α⊗language.␈α⊗ However␈α⊗constructs␈α↔like␈α⊗functional
␈↓ ↓H␈↓arguments␈αappear␈αin␈αseveral␈αprogramming␈αlanguages␈αunder␈αdifferent␈αguises.␈αUsually␈αthe␈αsyntax␈αof
␈↓ ↓H␈↓the␈α⊂language␈α⊂is␈α⊂sufficiently␈α⊂obfuscatory␈α⊂that␈α∂the␈α⊂true␈α⊂behavior␈α⊂and␈α⊂implications␈α⊂of␈α⊂devices␈α∂like
␈↓ ↓H␈↓functional␈αarguments␈αis␈αmisunderstood.␈αFaulty␈αimplementations␈αusually␈αresult.␈αIn␈αLISP␈αthe␈αproblem
␈↓ ↓H␈↓␈↓↓and the solution␈↓ appear with exceptional clarity␈↓π 69␈↓.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 69␈↓ Indeed, LISP was the first language to allow functional values.
␈↓ ↓H␈↓␈↓↓130  Evaluation␈↓ &4.10␈↓


␈↓ ↓H␈↓Here is a sketch of the abstract structure of the current ␈↓αeval␈↓.

␈↓ ↓H␈↓αeval <= λ[[exp;environ]
␈↓ ↓H␈↓α␈↓ αx[isvar[exp] → lookup[exp;environ];
␈↓ ↓H␈↓α␈↓ αx isconst[exp] → denote[exp];
␈↓ ↓H␈↓α␈↓ αx iscond[exp] → evcond[exp;environ];
␈↓ ↓H␈↓α␈↓ αx isfun[exp] → makefunarg[exp;environ];
␈↓ ↓H␈↓α␈↓ αx isfunc+args[exp] → apply[func[exp];evlis[arglist[exp];environ];environ]] ]

␈↓ ↓H␈↓α␈↓where:␈↓α
␈↓ ↓H␈↓αapply <= λ[[fn;args,environ]
␈↓ ↓H␈↓α␈↓ αx[isfunname[fn] →  ...;
␈↓ ↓H␈↓α␈↓ αx islambda[fn] → eval[body[fn];mkenv[vars[fn];args;environ]];
␈↓ ↓H␈↓α␈↓ αx isfunarg[fn] → apply[func␈↓β1␈↓α[fn];args;evn[fn]];

␈↓ ↓H␈↓α␈↓ αx       ...            ... ]]


␈↓ ↓H␈↓The␈α
reader␈αis␈α
encouraged␈αto␈α
complete␈αthe␈α
definitions,␈αsupplying␈α
appropriate␈α
constructors,␈αselectors
␈↓ ↓H␈↓and recognizers.

␈↓ ↓H␈↓Now␈α
for␈α
some␈α
specific␈αexamples.␈α
 In␈α
particular,␈α
most␈α
implementations␈αof␈α
LISP␈α
include␈α
a␈αvery␈α
useful
␈↓ ↓H␈↓class of mapping functions.

␈↓ ↓H␈↓␈↓αmaplist␈↓␈αis␈αa␈αfunction␈αof␈αtwo␈αarguments,␈αa␈α
list␈α␈↓αl␈↓␈αand␈α␈↓αfn␈↓,␈αa␈αfunctional␈αargument.␈α␈↓αmaplist␈↓␈α
applies␈αthe
␈↓ ↓H␈↓␈↓ αhfunction␈α␈↓αfn␈↓␈α(of␈αone␈αargument)␈αto␈αthe␈αlist␈α␈↓αl␈↓␈αand␈αits␈αtails␈α(␈↓αrest[l],␈αrest[rest[l]], ..␈↓)␈αuntil␈α␈↓αl␈↓␈αis
␈↓ ↓H␈↓␈↓ αhreduced␈αto␈α␈↓α( )␈↓.␈α The␈αvalue␈αof␈α␈↓αmaplist␈↓␈αis␈αthe␈αlist␈αof␈αthe␈αvalues␈αreturned␈αby␈α␈↓αfn␈↓.␈α Here's␈αa
␈↓ ↓H␈↓␈↓ αhdefinition of ␈↓αmaplist␈↓:

␈↓ ↓H␈↓␈↓ β∪␈↓αmaplist <= λ[[fn;l][null[l] → ( ); ␈↓
t␈↓α → concat[fn[l];maplist[fn;rest[l]]]]]␈↓. 

␈↓ ↓H␈↓Thus:

␈↓ ↓H␈↓␈↓α␈↓ α|maplist[function[reverse];(A B C D)] = ((D C B A)(D C B)(D C)(D))␈↓. ␈↓π 70␈↓ 

␈↓ ↓H␈↓An␈α
interesting␈α
and␈α
non-trivial␈α
use␈α
of␈α
functional␈α
arguments␈α
is␈α
shown␈α
on␈α
page 146␈α
where␈α
we␈α
define␈α
a
␈↓ ↓H␈↓new control structure suitable for describing algorithms built to operate on lists.







␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 70␈↓ ␈↓αquote␈↓-ing ␈↓αreverse␈↓ would also work since ␈↓αreverse␈↓ uses no free variables.
␈↓ ↓H␈↓␈↓↓4.10␈↓ ε⊂Functional Arguments and Functional Values     131␈↓


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.  What changes should be made to the LISP syntax equations to allow functional arguments?

␈↓ ↓H␈↓II.␈α⊂Use␈α⊂␈↓αapp␈↓␈α⊂on␈α⊃page 125␈α⊂to␈α⊂define␈α⊂a␈α⊂function␈α⊃which␈α⊂computes␈α⊂factorial␈α⊂without␈α⊂using␈α⊃␈↓αlabel␈↓␈α⊂or
␈↓ ↓H␈↓explicit calls on the evaluator.

␈↓ ↓H␈↓III. Extend ␈↓αeval␈↓ and friends to handle functional arguments.

␈↓ ↓H␈↓IV.␈αAn␈αinteresting␈αuse␈αof␈αfunctional␈αarguments␈αinvolves␈αself-applicative␈αfunctions.␈α An␈αapplication
␈↓ ↓H␈↓of␈αa␈αfunction␈α␈↓αf␈↓␈αin␈αa␈αcontext␈α␈↓αf[...;f;...]␈↓␈αis␈αan␈αinstance␈αof␈αself␈αapplication␈α␈↓π 71␈↓.␈αSelf-applicative␈αfunctions
␈↓ ↓H␈↓can␈α⊃be␈α⊃used␈α∩to␈α⊃define␈α⊃recursive␈α⊃functions␈α∩in␈α⊃such␈α⊃a␈α⊃way␈α∩that␈α⊃the␈α⊃definition␈α⊃is␈α∩not␈α⊃␈↓↓statically␈↓
␈↓ ↓H␈↓self-referential,␈αbut␈αis␈α␈↓↓dynamically␈↓␈αre-entrant.␈α For␈αexample,␈αhere␈αis␈αour␈αcanonical␈αexample,␈αwritten
␈↓ ↓H␈↓using a self-applicative function:

␈↓ ↓H␈↓α␈↓ ¬∩fact <= λ[[n]f[function[f]; n]]

␈↓ ↓H␈↓α␈↓ ∧@f <= λ[[g;n][n=0 → 1; ␈↓
t␈↓α → *[n; g[g; n-1]] ]]

␈↓ ↓H␈↓Use Weizenbaum's environments to show the execution of ␈↓αfact[2]␈↓.



␈↓ ↓H␈↓␈↓ ∧ ␈↓↓4.11  Binding strategies and implementations␈↓


␈↓ ↓H␈↓After␈αthe␈αdiscussion␈αof␈αvariables␈αin␈αSection 4.7␈αand␈αthe␈αintervening␈αdiscussions␈αof␈αenvironments,␈αit
␈↓ ↓H␈↓should␈α
now␈α
be␈α
clear␈α
that␈αthe␈α
root␈α
of␈α
the␈α
binding␈α
problem␈αis␈α
free␈α
variables.␈α
We␈α
would␈α
rather␈αnot
␈↓ ↓H␈↓outlaw␈αfree␈αvariables;␈αmany␈αinteresting␈αrecursive␈αalgorithm␈αhave␈αfree␈αvariables.␈α We␈αdon't␈αwant␈αto
␈↓ ↓H␈↓restrict␈α∩the␈α∩use␈α∩of␈α∪free␈α∩variables␈α∩too␈α∩precipitously␈α∩since␈α∪they␈α∩are␈α∩a␈α∩very␈α∪useful␈α∩programming
␈↓ ↓H␈↓technique.␈αFor␈αexample,␈αthe␈αpossible␈αalternative␈αof␈αpassing␈αall␈αglobal␈αinformation␈αthrough␈αas␈αextra
␈↓ ↓H␈↓parameters in calling sequences is overly expensive␈↓π 72␈↓.

␈↓ ↓H␈↓Handling␈α∂of␈α∂free␈α⊂variables␈α∂varies␈α∂from␈α∂programming␈α⊂language␈α∂to␈α∂programming␈α⊂language.␈α∂ The
␈↓ ↓H␈↓solution␈α
advocated␈α
by␈α
Algol-like␈α
languages␈α
is␈α
called␈α
␈↓↓static␈α
binding␈↓␈α
and␈α
dictates␈α
that␈α
all␈α
non-local
␈↓ ↓H␈↓references␈α
be␈α
fixed␈α
in␈α
the␈α
binding␈α
environment;␈αthus␈α
free␈α
variables␈α
aren't␈α
really␈α
free␈α
in␈α
the␈αsense
␈↓ ↓H␈↓that␈α∂we␈α∞have␈α∂a␈α∂choice␈α∞to␈α∂make.␈α∂ LISP␈α∞at␈α∂least␈α∞gives␈α∂you␈α∂a␈α∞choice.␈α∂Using␈α∂␈↓αquote␈↓␈α∞you␈α∂will␈α∂get␈α∞the
␈↓ ↓H␈↓dynamic␈α∪binding␈α∪on␈α∪free␈α∪variables␈α∪in␈α∪a␈α∪functional␈α∪argument;␈α∪using␈α∪␈↓αfunction␈↓␈α∪gives␈α∪the␈α∩static



␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 71␈↓ provided the designated argument position is a functional argument.

␈↓ ↓H␈↓␈↓π 72␈↓ Though much of that expense can be mitigated by a clever compiler.
␈↓ ↓H␈↓␈↓↓132  Evaluation␈↓ (4.11␈↓


␈↓ ↓H␈↓interpretation␈↓π 73␈↓.␈α⊂ There␈α⊃are␈α⊂no␈α⊃questions␈α⊂about␈α⊃Algol's␈α⊂interpretation␈α⊃of␈α⊂functional␈α⊃values:␈α⊂the
␈↓ ↓H␈↓construct␈α∞is␈α∞not␈α∞allowed.␈α∞When␈α∞we␈α∞discuss␈α∞implementation␈α∞of␈α∞binding␈α∞strategies␈α∞in␈α∂Chapter 5␈α∞we
␈↓ ↓H␈↓will see why.

␈↓ ↓H␈↓The␈α∀binding␈α∀strategy␈α∪determines␈α∀␈↓↓when␈↓␈α∀the␈α∀variables␈α∪will␈α∀receive␈α∀values;␈α∀the␈α∪implementation
␈↓ ↓H␈↓determines␈α␈↓↓how␈↓␈αthose␈αbindings␈αare␈αto␈αbe␈αaccomplished␈αand␈αtherefore,␈αhow␈αthe␈αlookup␈αof␈α
values␈αis
␈↓ ↓H␈↓to␈α be␈α!accomplished.␈α  We␈α have␈α!seen␈α one␈α implementation␈α!in␈α the␈α!␈↓αassoc - pairlis␈↓␈α pair
␈↓ ↓H␈↓(Section 4.5, page 106),␈α⊂and␈α⊃on␈α⊂page 117␈α⊂suggested␈α⊃a␈α⊂related␈α⊂implementation␈α⊃using␈α⊂Weizenbaum
␈↓ ↓H␈↓environments. We propose to examine another implementation.

␈↓ ↓H␈↓The␈αmost␈αgeneral␈αenvironment␈αstructure␈αwhich␈αLISP␈αcreates␈αis␈αa␈αtree␈αof␈αlocal␈αsymbol␈αtables,␈αrooted
␈↓ ↓H␈↓in␈α∪the␈α∪global␈α∩table.␈α∪The␈α∪typical␈α∩LISP␈α∪computation␈α∪generates␈α∩a␈α∪single␈α∪branch,␈α∪but␈α∩functional
␈↓ ↓H␈↓arguments␈αand␈αvalues␈αcan␈αgenerate␈αseveral␈αbranches.␈αLocating␈αa␈αvariable␈α␈↓αn␈↓␈αinvolved␈αsearching␈αthe
␈↓ ↓H␈↓current␈α
branch␈α
from␈αtip␈α
to␈α
root,␈α
looking␈αfor␈α
the␈α
first␈α
occurrence␈αof␈α
␈↓αn␈↓.␈α
If␈α
␈↓αn␈↓␈αwas␈α
bound␈α
very␈αdeep,␈α
the
␈↓ ↓H␈↓search␈α
could␈α
be␈αlong.␈α
 Indeed␈α
the␈α
time␈αis␈α
proportional␈α
to␈αthe␈α
depth␈α
of␈α
the␈αbranch.␈α
It␈α
has␈αbeen␈α
noted
␈↓ ↓H␈↓[Wegb 75]␈αthat␈αvariables␈αtend␈αto␈α
be␈αrebound␈αrather␈αseldom;␈αthere␈α
are␈αfew␈αoccurrences␈αof␈αany␈α
given
␈↓ ↓H␈↓variable␈α∪on␈α∀any␈α∪particular␈α∀branch.␈α∪ If␈α∪this␈α∀is␈α∪the␈α∀case,␈α∪then␈α∪the␈α∀search␈α∪will␈α∀examine␈α∪many
␈↓ ↓H␈↓environment␈α
blocks␈α
which␈α
do␈α
not␈α
contain␈α
the␈αdesired␈α
variable.␈α
If␈α
the␈α
number␈α
of␈α
bindings␈α
of␈αany
␈↓ ↓H␈↓variable␈α
is␈α
small␈α
compared␈α
to␈αthe␈α
number␈α
of␈α
environment␈α
blocks␈αwhich␈α
have␈α
to␈α
be␈α
searched␈αto␈α
find
␈↓ ↓H␈↓those␈α⊂bindings,␈α∂then␈α⊂we␈α∂would␈α⊂like␈α∂a␈α⊂viable␈α∂alternative␈α⊂to␈α∂the␈α⊂␈↓αassoc - pairlis␈↓␈α⊂implementation␈α∂of
␈↓ ↓H␈↓␈↓αlookup - mkenv␈↓.␈α
 That␈α
is,␈α
a␈α
scheme␈α
whose␈α
search␈α
is␈α
proportional␈α
to␈α
the␈α
number␈α
of␈α
bindings␈α
for␈αa
␈↓ ↓H␈↓variable, rather than proportional to the depth of the tree.  There is such an alternative.

␈↓ ↓H␈↓Namely,␈α
we␈α
associate␈α␈↓↓all␈↓␈α
the␈α
values␈α
possessed␈αby␈α
␈↓αn␈↓␈α
with␈α
␈↓αn␈↓␈αitself.␈α
 To␈α
signify␈α
which␈αenvironment␈α
was
␈↓ ↓H␈↓current␈α⊗when␈α⊗each␈α⊗binding␈α⊗was␈α⊗made,␈α⊗we␈α⊗associate␈α⊗pairs␈α⊗consiting␈α⊗of␈α⊗the␈α⊗value␈α⊗and␈α∃the
␈↓ ↓H␈↓environment␈αname.␈α Thus␈αthe␈αnew␈α␈↓αmkenv[vars;vals;env]␈↓␈αapplication␈αmust␈αname␈αa␈αnew␈αenvironment
␈↓ ↓H␈↓(call␈α∞it␈α
␈↓αnew␈↓);␈α∞attach␈α∞␈↓αnew␈↓␈α
to␈α∞the␈α
tip␈α∞of␈α∞the␈α
current␈α∞branch␈α
in␈α∞the␈α∞environment␈α
tree.␈α∞Also,␈α∞for␈α
each
␈↓ ↓H␈↓entry in ␈↓αvals␈↓, ␈↓αmkenv␈↓ must associate a value-␈↓αnew␈↓ pair with each name in ␈↓αvars␈↓.

␈↓ ↓H␈↓The␈α␈↓αlookup␈↓␈αprocedure␈αis␈αgiven␈αthe␈αname␈α␈↓αn␈↓␈α
and␈αa␈αbranch␈αin␈αthe␈αenvironment␈αtree.␈αAssume␈αthat␈α
the
␈↓ ↓H␈↓tip␈αnode␈αof␈αthe␈αtree␈α
is␈αnamed␈αEnv.␈α If␈α␈↓αn␈↓␈αhas␈α
an␈αattached␈αvalue␈αpair␈αwhose␈αenvironment␈α
component
␈↓ ↓H␈↓is␈α⊂Env,␈α⊂then␈α⊂the␈α⊂associated␈α⊂value␈α⊂is␈α⊂returned␈α⊂by␈α⊂␈↓αlookup␈↓.␈α⊂Otherwise␈α⊂the␈α⊂environment␈α⊃branch␈α⊂is
␈↓ ↓H␈↓searched␈α
recursively␈α
by␈α
␈↓αlookup␈↓␈α
looking␈α
for␈α
the␈α
first␈α
node␈α
in␈α
that␈α
branch␈α
which␈α
has␈α∞an␈α
associated
␈↓ ↓H␈↓value attached to ␈↓αn␈↓.






␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 73␈↓␈αA␈αcase␈αcan␈αbe␈αmade␈αfor␈αeven␈αmore␈αflexibility␈αin␈αthe␈αinterpretation␈αof␈αfree␈αvariables.␈α
We␈αcould
␈↓ ↓H␈↓ask␈α
that␈α
the␈α
binding␈αbe␈α
done␈α
on␈α
a␈αper␈α
variable␈α
basis.␈α
That␈α
is␈αwe␈α
could␈α
declare␈α
which␈αfree␈α
variables
␈↓ ↓H␈↓are␈α∞to␈α∞be␈α∞captured␈α∞statically␈α∞and␈α∞which␈α∞are␈α∞to␈α∞be␈α∞captured␈α∞dynamically.␈α∞ We␈α∞could␈α∞also␈α∞ask␈α
that
␈↓ ↓H␈↓␈↓↓both␈↓␈α
bindings␈αbe␈α
available␈αand␈α
supply␈α
selectors␈αwhich␈α
would␈αaccess␈α
either␈α
the␈αdynamic␈α
or␈αthe␈α
static
␈↓ ↓H␈↓binding.
␈↓ ↓H␈↓%24.11␈↓ ε]Binding strategies and implementations     133%*



␈↓ ↓H␈↓Here's a graphical description of this reorganization of our symbol tables:

␈↓"␈↓ ↓H␈↓α␈↓ ¬≡␈↓↓ The ␈↓αassoc-pairlis ␈↓↓structure
␈↓"␈↓ ↓H␈↓
    E1              E2             E4
␈↓"␈↓ ↓H␈↓
⊂αααπααα⊃       ⊂αααπααα⊃      ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
~ x ~ 3 ~       ~ y ~ 4 ~      ~ x ~ C ~
␈↓"␈↓ ↓H␈↓
εαααβαααλ       %αααβααα$      %αααβααα$
␈↓"␈↓ ↓H␈↓
~ y ~ 1 ~           ~              ↓
␈↓"␈↓ ↓H␈↓
εαααβαααλ           ↓              E3
␈↓"␈↓ ↓H␈↓
~ z ~ A ~           ~          ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
%αααβααα$           ~          ~ z ~ 4 ~
␈↓"␈↓ ↓H␈↓
    ~               ~          %αααβααα$
␈↓"␈↓ ↓H␈↓
    ↓               ↓              ↓
␈↓"␈↓ ↓H␈↓
    ~               E0             ~
␈↓"␈↓ ↓H␈↓
    %ααααα→αααα→⊂αααπααα⊃←αααα←αααα$
␈↓"␈↓ ↓H␈↓
                ~ y ~ 2 ~
␈↓"␈↓ ↓H␈↓
                εαααβαααλ
␈↓"␈↓ ↓H␈↓
                ~ z ~ 1 ~
␈↓"␈↓ ↓H␈↓
                %ααα∀ααα$

␈↓"␈↓ ↓H␈↓↓␈↓ ¬KThe new  structures
␈↓"␈↓ ↓H␈↓
                   x              y             z
␈↓"␈↓ ↓H␈↓
E1      E4    ⊂ααααπααα⊃     ⊂ααααπααα⊃    ⊂ααααπααα⊃
␈↓"␈↓ ↓H␈↓
 ↓  E2  ↓     ~ E1 ~ 3 ~     ~ E0 ~ 2 ~    ~ E0 ~ 1 ~
␈↓"␈↓ ↓H␈↓
 \  ~  E3     εααααβαααλ     εααααβαααλ    εααααβαααλ
␈↓"␈↓ ↓H␈↓
  \ ↓ /       ~ E4 ~ C ~     ~ E1 ~ 1 ~    ~ E1 ~ A ~
␈↓"␈↓ ↓H␈↓
   \~/        %αααα∀ααα$     εααααβαααλ    εααααβαααλ
␈↓"␈↓ ↓H␈↓
    ↓                        ~ E2 ~ 4 ~    ~ E3 ~ 4 ~
␈↓"␈↓ ↓H␈↓
    E0                       %αααα∀ααα$    %αααα∀ααα$

␈↓ ↓H␈↓Using the discussion of ␈↓αmkenv␈↓ on page 106, we define:
␈↓ ↓H␈↓α␈↓ ¬>alloc <=λ[[x] gensym[]]

␈↓ ↓H␈↓α␈↓ ∧[link <= λ[[block;env] concat[block;env]]

␈↓ ↓H␈↓α␈↓ ∧↔send <= λ[[n;v;blk] addval[n;mkent[first[blk];v]]]

␈↓ ↓H␈↓The␈αfunction␈α␈↓αgensym␈↓␈αis␈αto␈αgenerate␈αa␈αnew␈αenvironment␈αname.␈αArmed␈αwith␈αthat,␈α␈↓αalloc␈↓␈αmakes␈αa␈αnew
␈↓ ↓H␈↓tip node on the end of the current environment branch.

␈↓ ↓H␈↓The function ␈↓αaddval␈↓ adds a new entry to the variable ␈↓αn␈↓. The value of ␈↓αaddval␈↓ is ␈↓αenv␈↓.
␈↓ ↓H␈↓␈↓↓134  Evaluation␈↓ (4.11␈↓



␈↓ ↓H␈↓The␈α⊂new␈α⊂␈↓αlookup␈↓␈α⊂is␈α⊂more␈α∂complicated␈α⊂than␈α⊂the␈α⊂simple␈α⊂␈↓αassoc␈↓.␈α∂Given␈α⊂a␈α⊂node␈α⊂in␈α⊂the␈α∂environment
␈↓ ↓H␈↓branch,␈αwe␈αmust␈αsee␈αif␈αthere␈α
is␈αa␈αrelated␈αbinding␈αfor␈α␈↓αn␈↓.␈αIf␈α
there␈αis,␈αthen␈αthat's␈αthe␈αbinding␈αwe␈α
want.
␈↓ ↓H␈↓If no binding is found we look at the next deeper node on the tree, and check its bindings.

␈↓ ↓H␈↓αlookup <= λ[[n;env] λ[[z]look␈↓λ'␈↓α[z;z;env]][getval[n]] ]

␈↓ ↓H␈↓αlook␈↓λ'␈↓α <= λ[[l;l1;env]␈↓ βX[null[l] →look␈↓λ'␈↓α[l1;l1;rest[env]]
␈↓ ↓H␈↓α␈↓ βX eq[name[first[l]];first[env]] → value[first[l]]
␈↓ ↓H␈↓α␈↓ βX ␈↓
t␈↓α → look␈↓λ'␈↓α[rest[l];l1;env] ]]


␈↓ ↓H␈↓This␈α∞new␈α
scheme␈α∞is␈α
called␈α∞␈↓↓shallow␈α
binding␈↓,␈α∞and␈α
the␈α∞␈↓αassoc-pairlis␈↓␈α
scheme␈α∞is␈α
called␈α∞␈↓↓deep␈α
binding␈↓.
␈↓ ↓H␈↓The␈αessential␈αdifferences␈αbetween␈αthese␈αtwo␈αbinding␈αimplementations␈αis␈αthat␈αa␈αdeep␈αbinding␈αsearch
␈↓ ↓H␈↓is␈α∪keyed␈α∪on␈α∪the␈α∪name␈α∪of␈α∪the␈α∀variable,␈α∪whereas␈α∪a␈α∪shallow␈α∪binding␈α∪strategy␈α∪is␈α∪keyed␈α∀on␈α∪the
␈↓ ↓H␈↓environment␈α∂name.␈α∂These␈α∂requirements␈α∂have␈α∞corresponding␈α∂implications␈α∂for␈α∂the␈α∂organization␈α∞of
␈↓ ↓H␈↓the␈α
symbol␈α
tables.␈α∞ We␈α
will␈α
discuss␈α
the␈α∞relative␈α
merits␈α
of␈α
these␈α∞implementations␈α
in␈α
more␈α∞detail␈α
in
␈↓ ↓H␈↓Chapter 5.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Suggest an implementation of ␈↓αaddval␈↓ which will improve the search efficiency of ␈↓αlookup␈↓.

␈↓ ↓H␈↓II.␈α
 Analyze␈α
␈↓αlookup␈↓␈α
in␈α
a␈αmanner␈α
similar␈α
to␈α
that␈α
performed␈αon␈α
␈↓αmkenv␈↓␈α
on␈α
page 106.␈α
Identify␈αthe␈α
parts
␈↓ ↓H␈↓of␈α∂␈↓αlookup␈↓␈α∂which␈α∂are␈α∂independent␈α∂of␈α∞the␈α∂binding␈α∂implementation.␈α∂Rewrite␈α∂the␈α∂shallow␈α∂and␈α∞deep
␈↓ ↓H␈↓versions of ␈↓αlookup␈↓ in this more general setting.



␈↓ ↓H␈↓␈↓ ¬N␈↓↓4.12  Special Forms␈↓


␈↓ ↓H␈↓We␈α∪have␈α∪remarked␈α∀that␈α∪the␈α∪evaluation␈α∀scheme␈α∪for␈α∪LISP␈α∀functions␈α∪is␈α∪call-by-value␈α∀and,␈α∪for
␈↓ ↓H␈↓functions␈α⊂with␈α⊂multiple␈α⊂arguments,␈α∂left-to-right␈α⊂evaluation␈α⊂of␈α⊂arguments.␈α∂We␈α⊂have␈α⊂also␈α⊂seen,␈α∂in
␈↓ ↓H␈↓␈↓αquote␈↓␈α∂and␈α∂␈↓αcond␈↓,␈α∂that␈α∂not␈α∂all␈α∂forms␈α∂to␈α∞be␈α∂evaluated␈α∂in␈α∂LISP␈α∂fall␈α∂within␈α∂this␈α∂category.␈α∂ We␈α∞have
␈↓ ↓H␈↓already␈α
noted␈α
on␈α
page 92␈α
that␈α
␈↓αQUOTE␈↓␈α
and␈α∞␈↓αCOND␈↓␈α
are␈α
␈↓↓not␈↓␈α
translations␈α
of␈α
functions␈α
in␈α∞the␈α
LISP
␈↓ ↓H␈↓sense.␈α∂Indeed␈α∂the␈α∂purpose␈α∂of␈α∂␈↓αquote␈↓␈α∂was␈α⊂to␈α∂␈↓↓stop␈↓␈α∂evaluation.␈α∂ Also,␈α∂the␈α∂"argument␈α∂list"␈α∂to␈α⊂␈↓αcond␈↓␈α∂is
␈↓ ↓H␈↓handled␈α∩differently;␈α∩its␈α∩evaluation␈α⊃was␈α∩handled␈α∩by␈α∩␈↓αevcond␈↓.␈α⊃ Since␈α∩␈↓αquote␈↓␈α∩and␈α∩␈↓αcond␈↓␈α∩were␈α⊃rather
␈↓ ↓H␈↓anomalous␈α⊂we␈α∂have␈α⊂called␈α∂them␈α⊂␈↓↓special␈α⊂forms␈↓.␈α∂However,␈α⊂now␈α∂we␈α⊂would␈α∂like␈α⊂to␈α⊂discuss␈α∂special
␈↓ ↓H␈↓forms as a generally useful technique.

␈↓ ↓H␈↓Consider␈α
the␈α∞predicates␈α
␈↓αand␈↓␈α
and␈α∞␈↓αor␈↓.␈α
We␈α
might␈α∞wish␈α
to␈α
define␈α∞␈↓αand␈↓␈α
to␈α
be␈α∞a␈α
binary␈α∞predicate␈α
such
␈↓ ↓H␈↓␈↓↓4.12␈↓ 	;Special Forms     135␈↓


␈↓ ↓H␈↓that␈α␈↓αand␈↓␈αis␈α
true␈αjust␈αin␈α
case␈α␈↓↓both␈↓␈αarguments␈αevaluate␈α
to␈α␈↓
t␈↓,␈αand␈α
define␈α␈↓αor␈↓␈αto␈α
be␈αbinary␈αand␈αfalse␈α
just
␈↓ ↓H␈↓in␈αcase␈α
both␈αarguments␈α
evaluate␈αto␈α
␈↓
f␈↓.␈α Notice␈α
two␈αpoints.␈α
First,␈αthere␈α
is␈αreally␈α
no␈αreason␈α
to␈αrestrict
␈↓ ↓H␈↓these␈α
predicates␈α
to␈α
be␈α
␈↓↓binary␈↓.␈αReplacing␈α
the␈α
words␈α
"binary"␈α
by␈α"n-ary"␈α
and␈α
"both"␈α
by␈α
"all"␈α
in␈αthe
␈↓ ↓H␈↓above␈αdescription␈αhas␈αthe␈αdesired␈αeffect.␈α
 Second,␈αif␈αwe␈αevaluate␈αthe␈αarguments␈αto␈α
these␈αpredicates
␈↓ ↓H␈↓in␈α
some␈αorder,␈α
say␈αleft-to-right,␈α
then␈α
we␈αcould␈α
immediately␈αdetermine␈α
that␈α
␈↓αand␈↓␈αis␈α
false␈αas␈α
soon␈αas␈α
we
␈↓ ↓H␈↓come␈α∞across␈α
an␈α∞argument␈α
which␈α∞evaluates␈α
to␈α∞␈↓
f␈↓;␈α
similarly␈α∞a␈α
call␈α∞on␈α
␈↓αor␈↓␈α∞for␈α
an␈α∞arbitrary␈α∞number␈α
of
␈↓ ↓H␈↓arguments␈αcan␈αbe␈αterminated␈αas␈αsoon␈αas␈αwe␈αevaluate␈αan␈αargument␈αgiving␈αvalue␈α␈↓
t␈↓.␈α But␈αif␈αwe␈αinsist
␈↓ ↓H␈↓that␈α␈↓αand␈↓␈α
and␈α␈↓αor␈↓␈αbe␈α
LISP␈α␈↓↓functions␈↓␈αwe␈α
can␈αtake␈α
advantage␈αof␈αneither␈α
of␈αthese␈αobservations.␈α
Rather
␈↓ ↓H␈↓we␈αwill␈αdefine␈α␈↓αand␈↓␈αand␈α␈↓αor␈↓␈αas␈αspecial␈αforms␈αand␈αhandle␈αthe␈αevaluation␈αourselves.␈αPresently,␈αthe␈αonly
␈↓ ↓H␈↓way to handle special forms is to make explicit modifications to ␈↓αeval␈↓.

␈↓ ↓H␈↓On␈α∃page 160␈α∃and␈α∃in␈α⊗Section 4.21␈α∃we␈α∃will␈α∃discuss␈α∃simple␈α⊗ways␈α∃to␈α∃add␈α∃such␈α⊗forms␈α∃without
␈↓ ↓H␈↓modifying ␈↓αeval␈↓, and in Section .  we will discuss efficiency considerations.
␈↓ ↓H␈↓α␈↓Recognizers for the predicates must be added to ␈↓αeval␈↓:

␈↓ ↓H␈↓α␈↓ ∧xisand[e] → evand[args[e];environ];
␈↓ ↓H␈↓α␈↓ ¬⊂isor[e] → evor[args[e];environ];
␈↓ ↓H␈↓α␈↓ ε&␈↓where:␈↓α

␈↓ ↓H␈↓αevand <= λ[[l;a]␈↓ βH[null[l]→ ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ βH eval[first[l];a] → evand[rest[l];a];
␈↓ ↓H␈↓α␈↓ βH ␈↓
t␈↓α → ␈↓
f␈↓α] ]

␈↓ ↓H␈↓αevor <= λ[[l;a]␈↓ βH[null[l] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ βH eval[first[l];a] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ βH ␈↓
t␈↓α → evor[rest[l];a]] ]



␈↓ ↓H␈↓Notice␈αthe␈αexplicit␈αcalls␈αon␈α␈↓αeval␈↓␈↓π 74␈↓.␈α This␈αis␈αexpensive,␈αbut␈αcannot␈αbe␈αhelped.␈α Later␈αwe␈αwill␈αshow␈αa
␈↓ ↓H␈↓less␈αcostly␈αway␈αto␈αhandle␈αthose␈α"non-functions"␈αwhich␈αhave␈αan␈αindefinite␈αnumber␈αof␈αarguments,␈αall
␈↓ ↓H␈↓of which are to be evaluated (see Section  on macros).



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I␈α∞What␈α∞is␈α
the␈α∞difference␈α∞between␈α
a␈α∞special␈α∞form␈α
and␈α∞call-by-name?␈α∞Can␈α
call-by-name␈α∞be␈α∞done␈α
in
␈↓ ↓H␈↓LISP (without redefining ␈↓αeval␈↓)?

␈↓ ↓H␈↓II␈α
␈↓αselect␈↓␈α
is␈αa␈α
special␈α
form␈α
to␈αbe␈α
called␈α
as:␈α␈↓αselect[q;q␈↓β1␈↓α;e␈↓β1␈↓α; ... ;q␈↓βn␈↓α;e␈↓βn␈↓α;e]␈↓␈α
and␈α
to␈α
be␈αevaluated␈α
as␈α
follows:␈α␈↓αq␈↓␈α
is

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 74␈↓␈α⊂Also␈α⊂notice␈α⊂that␈α∂the␈α⊂abstract␈α⊂versions␈α⊂of␈α∂␈↓αevand␈↓␈α⊂and␈α⊂␈↓αevor␈↓␈α⊂know␈α∂that␈α⊂the␈α⊂arguments␈α⊂are␈α∂also
␈↓ ↓H␈↓presented as a sequence. The structure of the recursion implies a left-to-right evaluation.
␈↓ ↓H␈↓␈↓↓136  Evaluation␈↓ &4.12␈↓


␈↓ ↓H␈↓evaluated;␈α
the␈α␈↓αq␈↓βi␈↓'s␈α
are␈αevaluated␈α
from␈αleft␈α
to␈αright␈α
until␈αone␈α
is␈αfound␈α
with␈αthe␈α
value␈αof␈α
␈↓αq␈↓.␈αThe␈α
value
␈↓ ↓H␈↓of␈α␈↓αselect␈↓␈αis␈αthe␈αvalue␈αof␈αthe␈αcorresponding␈α␈↓αe␈↓βi␈↓.␈αIf␈αno␈αsuch␈α␈↓αq␈↓βi␈↓␈αis␈αfound␈αthe␈αvalue␈αof␈α␈↓αselect␈↓␈αis␈αthe␈αvalue
␈↓ ↓H␈↓of␈α␈↓αe␈↓.␈α␈↓αselect␈↓␈αis␈αa␈αprecursor␈αof␈αthe␈α␈↓↓case␈αstatement␈↓;␈αsee␈αpage 143.␈α Add␈αa␈αrecognizer␈αto␈α␈↓αeval␈↓␈αto␈αhandle
␈↓ ↓H␈↓␈↓αselect␈↓ and write a function to perform the evaluation of ␈↓αselect␈↓.



␈↓ ↓H␈↓␈↓ ¬=␈↓↓4.13  The ␈↓αprog␈↓↓-feature␈↓α


␈↓ ↓H␈↓Though␈αrecursion␈αis␈αa␈α
significant␈αtool␈αfor␈αconstructing␈α
LISP␈αprograms,␈αthere␈αis␈α
another␈αtechnique
␈↓ ↓H␈↓for␈α
defining␈α
algorithms␈α
in␈αLISP.␈α
 It␈α
is␈α
an␈αiterative␈α
style␈α
of␈α
programming␈αwhich␈α
is␈α
called␈α
the␈α␈↓αprog␈↓␈α
or
␈↓ ↓H␈↓␈↓αprog␈↓ram feature.

␈↓ ↓H␈↓Many␈α∩algorithms␈α⊃present␈α∩themselves␈α⊃more␈α∩naturally␈α⊃as␈α∩iterative␈α⊃schemes.␈α∩ Recall␈α∩the␈α⊃recursive
␈↓ ↓H␈↓algorithms␈α∞␈↓αlength␈↓␈α
and␈α∞␈↓αlength␈↓β1␈↓␈α
given␈α∞on␈α
page 48.␈α∞These␈α
algorithms␈α∞computed␈α
the␈α∞length␈α
of␈α∞a␈α
list.
␈↓ ↓H␈↓Compare those schemes with the following:

␈↓ ↓H␈↓␈↓↓1.␈↓ Set a variable ␈↓αl␈↓ to the given list.  Set a variable ␈↓αc␈↓ to zero.

␈↓ ↓H␈↓␈↓↓2.␈↓ If the list is empty, return as value of the computation, the current value in ␈↓αc␈↓.

␈↓ ↓H␈↓␈↓↓3.␈↓ Otherwise, increment ␈↓αc␈↓ by one.

␈↓ ↓H␈↓␈↓↓4.␈↓ Set ␈↓αl␈↓ to the ␈↓αrest␈↓ of ␈↓αl␈↓.

␈↓ ↓H␈↓␈↓↓5.␈↓ Go to line 2.

␈↓ ↓H␈↓Here is a LISP ␈↓αprog␈↓ version of the algorithm:

␈↓ ↓H␈↓αlength <= λ[[x]prog[[l;c]
␈↓ ↓H␈↓α␈↓ β(␈↓ βHl ← x;
␈↓ ↓H␈↓α␈↓ β(␈↓ βHc ← 0;
␈↓ ↓H␈↓α␈↓ β(a␈↓ βH[null[l] → return[c]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHc ← c+1;
␈↓ ↓H␈↓α␈↓ β(␈↓ βHl ← rest[l];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHgo[a]] ]


␈↓ ↓H␈↓We␈α↔have␈α⊗introduced␈α↔several␈α⊗new␈α↔symbols,␈α↔formats,␈α⊗and␈α↔functions␈α⊗in␈α↔this␈α↔example.␈α⊗These
␈↓ ↓H␈↓innovations␈αmust␈αbe␈αexplained␈αbefore␈αthe␈αexample␈αis␈αcomplete.␈αFirst,␈αthe␈αbasic␈αsyntax␈αof␈αa␈α␈↓αprog␈↓␈αis
␈↓ ↓H␈↓given by:
␈↓ ↓H␈↓␈↓↓4.13␈↓ 	→The ␈↓αprog␈↓↓-feature     137␈↓α



␈↓ ↓H␈↓<prog>␈↓ β(::= ␈↓αprog␈↓[[<prog variables>]<prog body>]
␈↓ ↓H␈↓<prog body>␈↓ β(::= <prog element><prog body> | <prog element>
␈↓ ↓H␈↓<prog element>␈↓ β(::= <label> | <prog form>;
␈↓ ↓H␈↓<label>␈↓ β(::= <identifier>
␈↓ ↓H␈↓<prog form>␈↓ β(::= <application>
␈↓ ↓H␈↓␈↓ β(::= <conditional statement>
␈↓ ↓H␈↓␈↓ β(::= <assignment statement>
␈↓ ↓H␈↓␈↓ β(::= <return statement>
␈↓ ↓H␈↓␈↓ β(::= <go statement>
␈↓ ↓H␈↓<conditional statement>␈↓ ∧(::= <conditional expression>
␈↓ ↓H␈↓<assignment statement>␈↓ ∧(::= <identifier> ← <form>
␈↓ ↓H␈↓<return statement>␈↓ ∧(::= ␈↓αreturn␈↓[<form>]
␈↓ ↓H␈↓<go statement>␈↓ β(␈↓ ∧(::= ␈↓αgo␈↓[<form>]

␈↓ ↓H␈↓The␈α␈↓αprog␈↓␈αvariables,␈α␈↓αl␈↓␈αand␈α␈↓αc␈↓,␈αin␈αthe␈αexample,␈αare␈αlocal␈αvariables.␈αThey␈αact␈αjust␈αlike␈αλ-variables␈αwith
␈↓ ↓H␈↓an implied initialization to ␈↓α( )␈↓.  Thus ␈↓αprog␈↓s can be used recursively.

␈↓ ↓H␈↓The␈α␈↓αprog␈↓␈αbody␈α
is␈αa␈αsequence␈αof␈α
␈↓αprog␈↓ forms␈αand␈αlabels.␈α Each␈α
␈↓αprog␈↓ form␈αis␈αevaluated␈αin␈α
the␈αusual
␈↓ ↓H␈↓LISP␈α
manner,␈α
and␈α
since␈α
the␈α
␈↓αprog␈↓ body␈α
can␈α∞consist␈α
of␈α
a␈α
sequence␈α
of␈α
␈↓αprog␈↓ forms,␈α
the␈α∞␈↓αprog␈↓-body␈α
is
␈↓ ↓H␈↓evaluated from left-to-right.

␈↓ ↓H␈↓If␈α
the␈α∞intent␈α
of␈α∞the␈α
␈↓αprog␈↓␈α∞was␈α
simply␈α
to␈α∞execute␈α
the␈α∞sequence␈α
of␈α∞␈↓αprog␈↓ forms,␈α
in␈α∞left-to-right␈α
order,
␈↓ ↓H␈↓then ␈↓αprog␈↓ could be replaced by a much simpler construct like ␈↓αprogn␈↓:

␈↓ ↓H␈↓α␈↓ ¬+progn <= λ[[x␈↓β1␈↓α; ...; x␈↓βn␈↓α] x␈↓βn␈↓α]

␈↓ ↓H␈↓However␈αwe␈αwill␈αadd␈αconstructs␈αto␈αLISP␈αwhich␈αwill␈αallow␈αus␈αto␈αvary␈αthe␈αflow␈αof␈αcontrol␈αwithin␈αthe
␈↓ ↓H␈↓␈↓αprog␈↓ body.␈α∩It␈α⊃is␈α∩to␈α⊃this␈α∩end␈α⊃that␈α∩we␈α⊃use␈α∩labels.␈α⊃Before␈α∩we␈α⊃discuss␈α∩labels␈α⊃and␈α∩their␈α⊃associated
␈↓ ↓H␈↓operations,␈α⊂we␈α⊂wish␈α⊂to␈α∂discuss␈α⊂the␈α⊂more␈α⊂general␈α⊂character␈α∂of␈α⊂␈↓αprog␈↓ forms.␈α⊂ In␈α⊂LISP,␈α⊂every␈α∂form
␈↓ ↓H␈↓returns␈α⊂a␈α⊂value;␈α⊂programming␈α∂languages␈α⊂also␈α⊂have␈α⊂constructs␈α∂which␈α⊂are␈α⊂primarily␈α⊂executed␈α∂for
␈↓ ↓H␈↓their␈α⊂effect␈α⊂rather␈α∂than␈α⊂their␈α⊂value.␈α∂ Such␈α⊂constructs␈α⊂are␈α∂called␈α⊂␈↓↓statements␈↓.␈α⊂For␈α⊂example,␈α∂LISP
␈↓ ↓H␈↓implementations␈α∞include␈α∞a␈α∞unary␈α∞primitive␈α∞named␈α∞␈↓αprint␈↓␈α∂whose␈α∞effect␈α∞is␈α∞to␈α∞print␈α∞the␈α∞value␈α∂of␈α∞its
␈↓ ↓H␈↓argument␈α∞on␈α
the␈α∞current␈α
output␈α∞device.␈α∞This␈α
function␈α∞also␈α
returns␈α∞its␈α
evaluated␈α∞argument␈α∞as␈α
the
␈↓ ↓H␈↓value␈α⊂of␈α⊂the␈α⊂␈↓αprint␈↓␈α⊃statement.␈α⊂Thus␈α⊂mathematically,␈α⊂␈↓αprint␈↓␈α⊂acts␈α⊃like␈α⊂an␈α⊂identity␈α⊂function,␈α⊃but␈α⊂its
␈↓ ↓H␈↓execution␈α∀certainly␈α∀affects␈α∀the␈α∀programmer's␈α∀environment.␈α∀ Operations␈α∀which␈α∀can␈α∃have␈α∀such
␈↓ ↓H␈↓non-local effects are said to have ␈↓↓side-effects␈↓␈↓π 75␈↓.

␈↓ ↓H␈↓There␈α
is␈α
another␈α
common␈α
and␈α
useful␈α
programming␈α
construct␈α
we␈α
wish␈α
to␈α
introduce␈α
into␈α␈↓αprog␈↓s␈α
which
␈↓ ↓H␈↓has␈α
side-effects.␈α
 It␈α
is␈α
called␈α
the␈α
␈↓↓assignment␈α
statement␈↓.␈α
 As␈α
with␈α
all␈α
LISP␈α
constructs,␈αthe␈α
assignment
␈↓ ↓H␈↓returns a value, but we identify it as a statement since it is executed more for effect than for value.


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 75␈↓␈αWhether␈αthe␈αact␈αof␈αprinting␈αis␈αa␈αside-effect␈αor␈αthe␈αfact␈αthat␈α␈↓αprint␈↓␈αreturns␈αa␈αvalue␈αis␈αa␈α
side-effect
␈↓ ↓H␈↓depends on your point of view.
␈↓ ↓H␈↓␈↓↓138  Evaluation␈↓ &4.13␈↓


␈↓ ↓H␈↓In␈α
our␈α
example␈α
of␈α
␈↓αlength␈↓,␈αwe␈α
used␈α
an␈α
assignment␈α
to␈α
bind␈α␈↓αl␈↓␈α
to␈α
the␈α
value␈α
of␈α
␈↓αx␈↓␈αand␈α
to␈α
bind␈α
␈↓αc␈↓␈α
to␈α␈↓α0␈↓.␈α
 To
␈↓ ↓H␈↓evaluate␈α
an␈α
assignment,␈αwe␈α
first␈α
evaluate␈α
the␈αform;␈α
then␈α
the␈α
identifier␈αis␈α
located␈α
by␈α
searching␈αthe
␈↓ ↓H␈↓access␈α∞chain.␈α∂Thus␈α∞the␈α∞identifier␈α∂may␈α∞be␈α∞a␈α∂non-local␈α∞variable.␈α∞When␈α∂the␈α∞identifier␈α∞is␈α∂located␈α∞its
␈↓ ↓H␈↓current␈αvalue␈αis␈αreplaced␈αby␈α
the␈αvalue␈αof␈αthe␈αform.␈αNotice␈α
that␈αthis␈αis␈αa␈αdifferent␈αkind␈α
of␈αbinding
␈↓ ↓H␈↓than␈α∂that␈α∂previously␈α∞done␈α∂by␈α∂λ-binding.␈α∂In␈α∞λ-binding␈α∂we␈α∂always␈α∂associated␈α∞a␈α∂new␈α∂value␈α∂with␈α∞a
␈↓ ↓H␈↓newly␈α∞created␈α∞local␈α∞symbol␈α
table␈α∞as␈α∞we␈α∞entered␈α
the␈α∞λ-body.␈α∞ We␈α∞never␈α
changed␈α∞the␈α∞binding␈α∞of␈α
a
␈↓ ↓H␈↓variable,␈α⊃though␈α⊂we␈α⊃could␈α⊃achieve␈α⊂the␈α⊃effect␈α⊃by␈α⊂rebinding␈α⊃the␈α⊃variable.␈α⊂The␈α⊃semantics␈α⊃of␈α⊂the
␈↓ ↓H␈↓assigment␈α
involve␈α
changing␈α
the␈α
binding.␈α
Thus␈α
assignments␈α
to␈α
non-local␈α
variables␈α
can␈α∞have␈α
effect
␈↓ ↓H␈↓outside␈αthe␈α␈↓αprog␈↓.␈αAssignment␈αstatements␈αtherefore␈αhave␈αa␈αside-effect.␈α An␈αassignment␈αstatement␈α
also
␈↓ ↓H␈↓has a value. Its value is the value of the form on the right-hand-side.

␈↓ ↓H␈↓As␈α
we␈α
intimated␈α
earlier,␈α␈↓αprog␈↓␈α
introduces␈α
some␈α
new␈α
control␈αstructures␈α
so␈α
that␈α
the␈α
␈↓αprog␈↓ body␈αneed␈α
not
␈↓ ↓H␈↓be␈αexecuted␈αin␈αsimple␈αleft-to-right␈αorder.␈α The␈αcontrol␈αstructures␈αare:␈αthe␈αconditional␈αstatement,␈αthe
␈↓ ↓H␈↓␈↓αreturn␈↓ statement, and the ␈↓αgo␈↓ statement.

␈↓ ↓H␈↓Though␈α⊂conditional␈α⊂statements␈α⊂in␈α⊂␈↓αprog␈↓s␈α⊂have␈α∂the␈α⊂same␈α⊂syntax␈α⊂as␈α⊂conditional␈α⊂expressions,␈α∂their
␈↓ ↓H␈↓semantics␈αis␈αslightly␈αdifferent.␈αA␈αconditional␈αstatement␈αis␈αexecuted␈αin␈αthe␈αusual␈αmanner␈αunless␈αnone
␈↓ ↓H␈↓of␈α
the␈α∞predicate␈α
alternatives␈α∞is␈α
satisfied.␈α∞Recall␈α
that␈α
a␈α∞conditional␈α
expression␈α∞is␈α
undefined␈α∞in␈α
this
␈↓ ↓H␈↓case;␈αa␈αconditional␈αstatement␈αhowever␈αis␈αdefined,␈αreturns␈α␈↓α( )␈↓,␈αand␈αexecutes␈αthe␈αnext␈αstatement␈αin␈α
the
␈↓ ↓H␈↓␈↓αprog␈↓ body.  Thus in our ␈↓αlength␈↓ example:
␈↓ ↓H␈↓α␈↓ ¬O[null[l] → return[c]],

␈↓ ↓H␈↓if␈α
␈↓αl␈↓␈αis␈α
not␈αempty␈α
the␈α
␈↓αprog␈↓ body␈αcontinues␈α
at␈αthe␈α
next␈αstatement␈α
with␈α
the␈αassignment␈α
of␈α␈↓αrest[l]␈↓␈α
to␈α␈↓αl␈↓.␈α
If
␈↓ ↓H␈↓␈↓αl␈↓ ␈↓↓is␈↓ empty, then the statement ␈↓αreturn[c]␈↓ is executed.

␈↓ ↓H␈↓The␈α∞␈↓αreturn␈↓␈α∞statement␈α∞is␈α∞a␈α∞␈↓αprog␈↓␈α∞construct␈α∞similar␈α
in␈α∞effect␈α∞to␈α∞exiting␈α∞a␈α∞λ-expression.␈α∞It␈α∞is␈α∞used␈α
to
␈↓ ↓H␈↓leave␈αa␈α
␈↓αprog␈↓ body␈αand␈α
return␈αto␈αthe␈α
caller␈αof␈α
the␈α␈↓αprog␈↓.␈α
As␈αwe␈αleave␈α
the␈α␈↓αprog␈↓,␈α
the␈αbindings␈α
of␈αthe
␈↓ ↓H␈↓␈↓αprog␈↓ variables␈αare␈αremoved␈αas␈αare␈αthe␈αλ-bindings␈αmade␈αon␈αentry␈αto␈αthe␈α␈↓αprog␈↓.␈α The␈αvalue␈αreturned
␈↓ ↓H␈↓is␈α
the␈α
value␈α
of␈α
the␈α
argument␈α
to␈α
the␈α
␈↓αreturn␈↓␈α
statement.␈α
The␈α
␈↓αreturn␈↓␈α
statement␈α
may␈α
be␈α
nested␈α
within
␈↓ ↓H␈↓other LISP computation, as for example:
␈↓ ↓H␈↓α␈↓ ¬4concat[A;return[list[B]]].

␈↓ ↓H␈↓However␈α
the␈αevaluation␈α
of␈α
the␈α␈↓αreturn␈↓␈α
is␈α
to␈αtake␈α
effect␈α
immediately;␈αthe␈α
␈↓αconcat␈↓␈α
would␈αnever␈α
complete
␈↓ ↓H␈↓its␈α
operation.␈α
We␈α
would␈αreturn␈α
␈↓α(B)␈↓␈α
to␈α
the␈α
caller␈αof␈α
the␈α
enclosing␈α
␈↓αprog␈↓.␈α
A␈αbit␈α
of␈α
care␈α
is␈α
needed␈αin
␈↓ ↓H␈↓describing␈α∂the␈α∂meaning␈α∂of␈α∂␈↓αreturn␈↓:␈α∂we␈α∂look␈α∂for␈α∂the␈α∂latest␈α∂instance␈α∂of␈α∂an␈α∂entrance␈α∂to␈α∂a␈α⊂␈↓αprog␈↓␈α∂and
␈↓ ↓H␈↓return␈α≡from␈α∨that␈α≡␈↓αprog␈↓.␈α∨The␈α≡simplest␈α∨way␈α≡to␈α∨visualize␈α≡this␈α∨is␈α≡to␈α∨use␈α≡Weizenbaum
␈↓ ↓H␈↓environments (page 124).␈α∩We␈α∩search␈α∪the␈α∩␈↓↓control␈α∩chain␈↓,␈α∩looking␈α∪for␈α∩the␈α∩first␈α∩␈↓	Form␈↓␈α∪which␈α∩is
␈↓ ↓H␈↓␈↓αprog[[ ... ] ... ]␈↓.␈α∂We␈α∞then␈α∂restore␈α∂using␈α∞the␈α∂access␈α∞and␈α∂control␈α∂information␈α∞found␈α∂in␈α∂that␈α∞diagram.
␈↓ ↓H␈↓We will give a comprehensive example after discussing the ␈↓αgo␈↓ statement.

␈↓ ↓H␈↓The␈α␈↓αgo␈↓␈αstatement␈αis␈αused␈αin␈αconjunction␈αwith␈αlabels␈αto␈αdivert␈αthe␈αimplied␈αleft-to-right␈αexecution␈αof
␈↓ ↓H␈↓the␈α␈↓αprog␈↓ body.␈α Labels␈αreally␈αaren't␈αexecuted;␈αthey␈αare␈αused␈αto␈αname␈αstatements␈αin␈αa␈α␈↓αprog␈↓.␈α It␈αis␈αthe
␈↓ ↓H␈↓␈↓αgo␈↓␈α
statement␈α
which␈α
uses␈αthe␈α
label␈α
as␈α
a␈α
destination␈αfor␈α
transferring␈α
control.␈α
 Labels␈α
may␈αconflict␈α
with
␈↓ ↓H␈↓the␈αλ-variables␈αor␈α␈↓αprog␈↓ variables␈αsince␈αthe␈αevaluator␈αfor␈α␈↓αprog␈↓s␈αcan␈αresolve␈αthe␈αconflicts␈αby␈αcontext.
␈↓ ↓H␈↓Any␈α∪identifier␈α∪occurring␈α∪by␈α∪itself␈α∩in␈α∪a␈α∪␈↓αprog␈↓ body␈α∪is␈α∪a␈α∩label.␈α∪Any␈α∪identifier␈α∪occurring␈α∪in␈α∩an
␈↓ ↓H␈↓␈↓↓4.13␈↓ 	→The ␈↓αprog␈↓↓-feature     139␈↓α


␈↓ ↓H␈↓application␈αother␈α
than␈αa␈α
␈↓αgo␈↓ statement␈αis␈α
a␈αvariable␈α
and␈αits␈α
value␈αis␈α
searched␈αfor␈α
in␈αthe␈αaccess␈α
chain,
␈↓ ↓H␈↓whereas␈α
an␈α
identifier␈α
appearing␈α
in␈α
a␈α
␈↓αgo␈↓␈α
statement␈α
is␈α
interpreted␈α
as␈α
a␈α
label␈α
and␈α
searched␈α
for␈α
in␈α
a
␈↓ ↓H␈↓␈↓αprog␈↓ body.

␈↓ ↓H␈↓The␈α
␈↓αgo␈↓␈α
statement␈αis␈α
a␈α
little␈α
more␈αcomplicated␈α
than␈α
the␈α␈↓αreturn␈↓␈α
statement.␈α
 If␈α
the␈αargument␈α
to␈α
␈↓αgo␈↓␈αis␈α
an
␈↓ ↓H␈↓identifier␈α
then␈α
it␈αis␈α
interpreted␈α
as␈αa␈α
label;␈α
otherwise,␈α
the␈αargument␈α
is␈α
␈↓↓evaluated␈↓␈αand␈α
the␈α
result␈αof␈α
the
␈↓ ↓H␈↓evaluation␈α∩is␈α∩interpreted␈α∪as␈α∩a␈α∩label.␈α∩ Once␈α∪a␈α∩label␈α∩has␈α∩been␈α∪uncovered␈α∩as␈α∩the␈α∩result␈α∪of␈α∩this
␈↓ ↓H␈↓evaluation␈αwe␈αmust␈α
locate␈αa␈αstatment␈αin␈α
a␈α␈↓αprog␈↓␈αwhich␈α
has␈αthat␈αlabel␈αattached␈α
to␈αit.␈αOur␈αintention␈α
is
␈↓ ↓H␈↓to␈α⊂transfer␈α⊂control␈α⊂to␈α⊂that␈α∂statement.␈α⊂ We␈α⊂locate␈α⊂the␈α⊂labeled␈α∂statement␈α⊂as␈α⊂follows:␈α⊂we␈α⊂look␈α∂back
␈↓ ↓H␈↓through␈αthe␈αcontrol␈αchain␈αfor␈αthe␈αfirst␈α␈↓αprog␈↓␈αwhich␈αcontains␈αthe␈αlabel.␈αWhen␈αthe␈αlabel␈αis␈αfound␈αwe
␈↓ ↓H␈↓transfer␈αcontrol␈αto␈αthat␈αlabeled␈αstatement,␈αrestoring␈αthe␈αaccess␈αand␈αcontrol␈αenvironments␈αof␈αthe␈α␈↓αprog␈↓
␈↓ ↓H␈↓which␈αcontain␈αthat␈αstatement.␈αThus␈αthere␈αis␈αa␈αdouble␈αsearch␈αinvolved:␈αwe␈αsearch␈αthe␈αcontrol␈αchain
␈↓ ↓H␈↓for␈α∞␈↓αprog␈↓␈α∂forms,␈α∞and␈α∞search␈α∂the␈α∞␈↓αprog␈↓␈α∞forms␈α∂for␈α∞the␈α∞label.␈α∂ Labels␈α∞need␈α∞not␈α∂be␈α∞local;␈α∞we␈α∂find␈α∞the
␈↓ ↓H␈↓closest dynamically surrounding ␈↓αprog␈↓ which contains the label.

␈↓ ↓H␈↓Notice␈αthat␈α␈↓αgo␈↓␈αand␈α␈↓αreturn␈↓␈αare␈αthe␈αfirst␈αconstructs␈αwe␈αhave␈αseen␈αwhose␈αbehavior␈αon␈αcompletion␈αdoes
␈↓ ↓H␈↓not imply that we restore the previous control environment.

␈↓ ↓H␈↓Finally, as an example covering the new features of ␈↓αprog␈↓ consider:

␈↓ ↓H␈↓α␈↓ βxf <= λ[[y;z]prog[[l;x]
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬H␈↓ ¬hl ← 2;
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬Hl␈↓ ¬hu ← g[y;x;z]
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬H␈↓ ¬h   ... ]]

␈↓ ↓H␈↓α␈↓ βxg <= λ[[x;y;z]prog[[ ]
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬H␈↓ ¬h ...
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬H␈↓ ¬hgo[l]
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬H␈↓ ¬h ...
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬H␈↓ ¬hreturn[first[x]]
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬H␈↓ ¬h ... ]]


␈↓ ↓H␈↓Notice␈αin␈α␈↓αf␈↓,␈α␈↓αl␈↓␈αis␈αboth␈αa␈αlabel␈αand␈αa␈α
␈↓αprog␈↓ variable.␈α Notice␈αin␈α␈↓αg␈↓␈αthat␈αwe␈αhave␈αno␈α␈↓αprog␈↓␈αvariables;␈α
and
␈↓ ↓H␈↓since we assume that ␈↓αl␈↓ is not a label in ␈↓αg␈↓ we have a non-local ␈↓αgo␈↓.

␈↓ ↓H␈↓Consider the evaluation of ␈↓αf[(A B);3]␈↓.

␈↓ ↓H␈↓␈↓ αH␈↓ β_␈↓αf[(A B);3]␈↓ ∧8␈↓ ¬x␈↓ εHprog[[l;x]...]␈↓ πh␈↓ λ8␈↓ 	λ[l ← 2; l u ← g[y;x;z];...]
␈↓ ↓H␈↓α␈↓ αH␈↓ β_␈↓E␈↓β0␈↓␈↓ ∧8␈↓ ¬x␈↓ εHE␈↓β1␈↓␈↓ πh␈↓ λ8␈↓ 	λE␈↓β2␈↓
␈↓ ↓H␈↓␈↓ αH /␈↓ β_| /␈↓ ∧8␈↓ ¬x   E␈↓β0␈↓␈↓ εH|  E␈↓β0␈↓␈↓ πh␈↓ λ8  E␈↓β1␈↓␈↓ 	λ|  E␈↓β1␈↓
␈↓ ↓H␈↓␈↓ αH_______␈↓ ∧8=>␈↓ ¬x_______␈↓ πh=>␈↓ λ8_______     =>
␈↓ ↓H␈↓␈↓ αH␈↓α f␈↓ β_| λ[[y;z]prog[...]]␈↓ ¬x y␈↓ εH| (A B)␈↓ πh␈↓ λ8 l␈↓ 	λ| ( )
␈↓ ↓H␈↓α␈↓ αH g␈↓ β_| λ[[x;y;z]prog[...]]␈↓ ¬x z␈↓ εH| 3␈↓ πh␈↓ λ8 x␈↓ 	λ| ( )
␈↓ ↓H␈↓␈↓↓140  Evaluation␈↓ &4.13␈↓


␈↓ ↓H␈↓At␈α∂this␈α∂point␈α∂we␈α∂have␈α∂done␈α∂the␈α∂λ-binding␈α∂and␈α∂initialized␈α∂the␈α∂␈↓αprog␈↓ variables.␈α∂ As␈α∂we␈α⊂begin␈α∂the
␈↓ ↓H␈↓execution␈αof␈αthe␈α
␈↓αprog␈↓ body,␈αwe␈αassign␈α
␈↓α2␈↓␈αto␈α␈↓αl␈↓␈α
and,␈αsince␈αlabels␈α
have␈αno␈αcomputational␈α
effect,␈αbegin
␈↓ ↓H␈↓the evaluation of the assignment statement: ␈↓αu ← g[y;x;z]␈↓:

␈↓ ↓H␈↓␈↓ β(␈↓ ∧(␈↓α[... l u ← g[y;x;z];...]␈↓ ε_␈↓ εh␈↓ π_[...u ← g[y;x;z]; ...]
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧(E␈↓β2␈↓α␈↓ ε_␈↓ εh␈↓ π_E␈↓β2␈↓α
␈↓ ↓H␈↓α␈↓ β(   E␈↓β1␈↓α␈↓ ∧(| E␈↓β1␈↓α␈↓ ε_␈↓ εh E␈↓β1␈↓α␈↓ π_| E␈↓β1␈↓α
␈↓ ↓H␈↓α␈↓ β(    __________␈↓ ε_=>␈↓ εh_________
␈↓ ↓H␈↓α␈↓ β(     l␈↓ ∧(| 2␈↓ ε_␈↓ εhl␈↓ π_| 2
␈↓ ↓H␈↓α␈↓ β(     x␈↓ ∧(| ( )␈↓ ε_␈↓ εhx␈↓ π_| ( )

␈↓ ↓H␈↓We evaluate ␈↓αg[y;x;z]␈↓:

␈↓ ↓H␈↓␈↓ β(␈↓ ∧(␈↓αprog[[ ] ...]␈↓ ε_␈↓ εh[...go[l]; ... return[first[x]]; ...]
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧(E␈↓β3␈↓α␈↓ ε_␈↓ εh␈↓ π_E␈↓β4␈↓α
␈↓ ↓H␈↓α␈↓ β(   E␈↓β2␈↓α␈↓ ∧(| E␈↓β2␈↓α␈↓ ε_␈↓ εh E␈↓β3␈↓α␈↓ π_| E␈↓β3␈↓α
␈↓ ↓H␈↓α␈↓ β(    __________␈↓ ε_=>␈↓ εh_________
␈↓ ↓H␈↓α␈↓ β(     x␈↓ ∧(| (A B)
␈↓ ↓H␈↓α␈↓ β(     y␈↓ ∧(| ( )
␈↓ ↓H␈↓α␈↓ β(     z␈↓ ∧(| 3

␈↓ ↓H␈↓The␈α␈↓αgo[l]␈↓␈αwill␈αsearch␈αthe␈αcontrol␈αchain;␈αit␈αlooks␈αat␈αthe␈α␈↓αprog␈↓␈αof␈αE␈↓β3␈↓␈αbut␈αfinds␈αno␈αlabel␈α␈↓αl␈↓.␈αIt␈αfinds␈αthe
␈↓ ↓H␈↓␈↓αprog␈↓␈αof␈αE␈↓β1␈↓␈αnext,␈αand␈αthere␈αit␈αdoes␈αfind␈αthe␈αlabel␈α␈↓αl␈↓.␈αThus␈αexecution␈αwould␈αbe␈αcontinued␈αin␈α
E␈↓β2␈↓,␈αthe
␈↓ ↓H␈↓environment␈αwhich␈αbound␈αthe␈α␈↓αprog␈↓ variables,␈αat␈αthe␈αassignment␈αstatement.␈αIn␈αgeneral,␈αwe␈αcontinue
␈↓ ↓H␈↓in the environment which was created on entry to the ␈↓αprog␈↓ body.

␈↓ ↓H␈↓Notice␈αthat␈αonce␈αwe␈αhave␈αleft␈αE␈↓β4␈↓␈αthere␈αis␈αno␈αway␈αto␈αjump␈αback␈αinto␈αit.␈α We␈αcan␈αonly␈αsearch␈αdown
␈↓ ↓H␈↓the␈α∞control␈α∞chain,␈α∞and␈α
the␈α∞entry␈α∞to␈α∞␈↓αg␈↓␈α
is␈α∞not␈α∞below␈α∞that␈α
of␈α∞␈↓αf␈↓␈α∞on␈α∞that␈α
chain.␈α∞ An␈α∞extension␈α∞of␈α
the
␈↓ ↓H␈↓semantics␈αof␈αLISP␈αcould␈αallow␈αsuch␈αgeneralized␈αcontrol␈αand␈αwe␈αwill␈αdevelop␈αsome␈αof␈αthose␈αideas␈αin
␈↓ ↓H␈↓Section 4.15.

␈↓ ↓H␈↓If␈αwe␈αexecuted␈αthe␈α␈↓αreturn[first[x]]␈↓␈αin␈αE␈↓β4␈↓␈αan␈αaction␈αsimilar␈αto␈αthat␈αof␈α␈↓αgo␈↓␈αwould␈αtranspire.␈αWe␈αwould
␈↓ ↓H␈↓evaluate␈α␈↓αfirst[x]␈↓,␈αgetting␈α␈↓αA␈↓.␈αWe␈αwould␈αsearch␈αthe␈αcontrol␈αchain␈αfor␈αthe␈α␈↓↓latest␈↓␈α␈↓αprog␈↓ expression;␈αhere
␈↓ ↓H␈↓found␈αin␈αE␈↓β3␈↓;␈αand␈αthen␈αreturn␈αcontrol␈α
to␈αthe␈αenvironment␈αdesignated␈αin␈αthe␈αcontrol␈α
quadrant;␈αhere
␈↓ ↓H␈↓E␈↓β2␈↓.␈α⊃Thus␈α⊃we␈α⊃return␈α⊃␈↓αA␈↓␈α⊃as␈α⊃the␈α⊃value␈α⊃of␈α∩␈↓αg[y;x;z]␈↓.␈α⊃Since␈α⊃the␈α⊃call␈α⊃on␈α⊃␈↓αg␈↓␈α⊃was␈α⊃a␈α⊃component␈α∩of␈α⊃the
␈↓ ↓H␈↓assignment␈α␈↓αu ← g[y;x;z]␈↓,␈α
we␈αmust␈αcomplete␈α
that␈αassignment.␈αWe␈α
search␈αthe␈α␈↓↓access␈↓␈α
chain␈αfor␈α␈↓αu␈↓.␈α
Since
␈↓ ↓H␈↓␈↓αu␈↓ is not found we make a global assignment in E␈↓β0␈↓:

␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬HE␈↓β0␈↓
␈↓ ↓H␈↓␈↓ ∧H␈↓ ¬H/| /␈↓β
␈↓ ↓H␈↓β␈↓ ∧H______________␈↓α
␈↓ ↓H␈↓α␈↓ ∧H      f␈↓ ¬H|  λ[[y;z] ...]
␈↓ ↓H␈↓α␈↓ ∧H      g␈↓ ¬H|  λ[[x;y;z] ...]
␈↓ ↓H␈↓α␈↓ ∧H      u␈↓ ¬H|  A
␈↓ ↓H␈↓␈↓↓4.13␈↓ 	→The ␈↓αprog␈↓↓-feature     141␈↓α


␈↓ ↓H␈↓The␈αability␈αto␈αevaluate␈αthe␈αargument␈αto␈α␈↓αgo␈↓␈αresults␈αin␈αa␈αuseful␈αprogramming␈αtrick.␈α Let␈α␈↓αl␈↓␈αbe␈αa␈αlist␈αof
␈↓ ↓H␈↓dotted␈αpairs,␈αeach␈αof␈αthe␈αform,␈α␈↓α(␈↓␈αobject␈↓βi␈↓␈α.␈αlabel␈↓βi␈↓α)␈↓.␈αAt␈αeach␈αlabel␈↓βi␈↓␈αwe␈αbegin␈αa␈αpiece␈αof␈αprogram␈α
to␈αbe
␈↓ ↓H␈↓executed when object␈↓βi␈↓ has been recognized.  Then the construct:

␈↓ ↓H␈↓␈↓	UGH␈↓␈↓ ¬a␈↓αgo[cdr[assoc[x;l]]]␈↓ 

␈↓ ↓H␈↓can␈αbe␈αused␈αto␈α"dispatch"␈αto␈αthe␈αappropriate␈αcode␈αwhen␈α␈↓αx␈↓␈αis␈αone␈αof␈αthe␈αobject␈↓βi␈↓.␈αThis␈αis␈αan␈αinstance
␈↓ ↓H␈↓of␈α␈↓↓table-driven␈↓␈αprogramming.␈α The␈αblocks␈αof␈αcode␈αdispatched␈αto␈αcan␈αbe␈αdistributed␈αthroughout␈αthe
␈↓ ↓H␈↓body␈αof␈αthe␈α
␈↓αprog␈↓.␈αEach␈αblock␈α
of␈αcode␈αwill␈α
usually␈αbe␈αfollowed␈α
by␈αa␈α␈↓αgo␈↓␈α
back␈αto␈αthe␈α
code␈αinvolving
␈↓ ↓H␈↓equation␈α
␈↓	UGH␈↓␈α
(above).␈αIn␈α
fact␈α
the␈αargument␈α
␈↓αl␈↓␈α
in␈α
␈↓	UGH␈↓␈αmay␈α
be␈α
␈↓↓global␈↓␈αto␈α
the␈α
␈↓αprog␈↓-body.␈α The␈α
effect
␈↓ ↓H␈↓is␈α
to␈α∞make␈α
a␈α
␈↓αprog␈↓␈α∞which␈α
is␈α∞very␈α
difficult␈α
to␈α∞understand.␈α
 The␈α
LISP␈α∞␈↓αselect␈↓␈α
(page 135)␈α∞will␈α
handle
␈↓ ↓H␈↓many␈αof␈αthe␈αpossible␈αapplications␈αof␈αthis␈αcoding␈αtrick␈αand␈αresult␈αin␈αa␈αmore␈αreadable␈αprogram.␈αThe
␈↓ ↓H␈↓case-statement␈α
(page 143)␈α
present␈α
in␈α
some␈α
other␈α
languages␈α
is␈α
also␈α
a␈α
better␈α
means␈α
of␈α∞handling␈α
this
␈↓ ↓H␈↓problem.

␈↓ ↓H␈↓The␈α∂␈↓αgo␈↓␈α∂statement␈α∂is␈α∂useful␈α⊂if␈α∂used␈α∂with␈α∂discretion.␈α∂It␈α⊂is␈α∂a␈α∂building␈α∂block␈α∂for␈α⊂constructing␈α∂more
␈↓ ↓H␈↓complex␈αcontrol␈αregimes,␈αparticularly␈αsince␈α
the␈αlabel␈αneed␈αnot␈αbe␈α
local␈αto␈αthe␈α␈↓αprog␈↓.␈αWe␈αwill␈α
examine
␈↓ ↓H␈↓some more complex kinds of control behavior in Section 4.15.

␈↓ ↓H␈↓Now to the problem of translating a ␈↓αprog␈↓ into an S-expression representation: the construct,

␈↓ ↓H␈↓␈↓ ∧I␈↓αprog[[v␈↓β1␈↓α; ...; v␈↓βn␈↓α] ...  ]␈↓ will be translated to: 

␈↓ ↓H␈↓␈↓ ¬;␈↓α(PROG(V1 ... VN) ...  )␈↓. 

␈↓ ↓H␈↓Notice␈α
that␈α
␈↓αprog␈↓␈α
is␈α
a␈α
special␈α
form.␈α
 So␈α
the␈α
body␈α
of␈α
the␈α
␈↓αprog␈↓␈α
must␈α
be␈α
handled␈α
specially␈α
by␈α∞a␈α
new
␈↓ ↓H␈↓piece of the evaluator.

␈↓ ↓H␈↓Similarly␈α∞we␈α
must␈α∞be␈α
careful␈α∞about␈α
the␈α∞interpretation␈α∞of␈α
←.␈α∞ We␈α
will␈α∞write␈α
␈↓αx␈α∞←␈α
y␈↓␈α∞in␈α∞prefix␈α
form:
␈↓ ↓H␈↓␈↓αsetq[x;y]␈↓. We will map this to:
␈↓ ↓H␈↓␈↓ ¬{␈↓α(SETQ X Y).␈↓

␈↓ ↓H␈↓Notice␈αthat␈α␈↓αsetq␈↓␈αis␈αalso␈αa␈αspecial␈αform.␈α For␈αif␈α␈↓αx␈↓␈αand␈α␈↓αy␈↓␈αhave␈αvalues␈α␈↓α2␈↓␈αand␈α␈↓α3␈↓,␈αfor␈αexample,␈αthen␈αthe
␈↓ ↓H␈↓call-by-value␈α
interpretation␈αof␈α
␈↓αsetq[x;y]␈↓␈αwould␈α
say␈α␈↓αsetq[2;3]␈↓.␈α
This␈αwas␈α
not␈αour␈α
intention.␈α
 We␈αwant
␈↓ ↓H␈↓to evaluate the second argument to ␈↓αsetq␈↓ while stopping the evaluation of the first argument.

␈↓ ↓H␈↓LISP␈αhas␈αanother␈αassignment-like␈αoperator␈αcalled␈α␈↓αset␈↓.␈α Both␈αarguments␈αof␈αthis␈αbinary␈αoperator␈αare
␈↓ ↓H␈↓evaluated;␈αthe␈αvalue␈αof␈αthe␈αfirst␈αargument␈αis␈αexpected␈αto␈αbe␈αa␈αrepresentation␈αof␈αa␈αvariable;␈αthat␈αis,
␈↓ ↓H␈↓the␈αfirst␈αargument␈αevaluates␈αto␈αa␈αliteral␈αatom.␈α The␈αsecond␈αargument␈αis␈αa␈αLISP␈αform␈αand␈αusing␈αthe
␈↓ ↓H␈↓value␈αof␈αthat␈αform,␈αan␈αassignment␈αis␈αmade␈αto␈αthe␈αvariable␈αrepresented␈αby␈αthe␈αfirst␈αargument.␈αThus
␈↓ ↓H␈↓we could define ␈↓αsetq␈↓ as:
␈↓ ↓H␈↓α␈↓ ¬	        setq[x;y] = set[quote[x];y]

␈↓ ↓H␈↓As␈α∂a␈α∞more␈α∂complex␈α∞example,␈α∂consider␈α∞␈↓αset[z;␈α∂plus[x;1]]␈↓.␈α∞ If␈α∂the␈α∞current␈α∂value␈α∞of␈α∂variable␈α∞␈↓αz␈↓␈α∂is␈α∞an
␈↓ ↓H␈↓identifier,␈αthen␈α␈↓αset[z;␈αplus[x;1]]␈↓␈αmakes␈αsense.␈α Assume␈αthe␈αcurrent␈αvalue␈αof␈α␈↓αZ␈↓,␈αthe␈αrepresentation␈αof
␈↓ ↓H␈↓␈↓↓142  Evaluation␈↓ &4.13␈↓


␈↓ ↓H␈↓␈↓αz␈↓,␈αis␈α␈↓αA␈↓;␈αand␈αassume␈αthe␈αcurrent␈αvalue␈αof␈α␈↓αx␈↓␈αis␈α␈↓α2␈↓;␈αthen␈αthe␈αeffect␈αof␈αthe␈α␈↓αset␈↓␈αstatement␈αis␈αto␈αassign␈αthe
␈↓ ↓H␈↓value ␈↓α3␈↓ to ␈↓αa␈↓.

␈↓ ↓H␈↓Normally␈αwhen␈α
you␈αare␈αmaking␈α
assignments,␈αyou␈αwant␈α
to␈αassign␈αto␈α
a␈α␈↓↓name␈↓␈αand␈α
not␈αa␈α
␈↓↓value␈↓;␈αthus
␈↓ ↓H␈↓you will tend to use the ␈↓αsetq␈↓ form.

␈↓ ↓H␈↓Finally, here is a translation of the body of the ␈↓αprog␈↓ version of ␈↓αlength:␈↓
␈↓ ↓H␈↓α␈↓ αX(LAMBDA (X)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH(PROG (L C)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ L X)
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ C 0)
␈↓ ↓H␈↓α␈↓ αX␈↓ βHA␈↓ βx(COND ((NULL L) (RETURN C)))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ C (ADD1 C))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(SETQ L (REST L))
␈↓ ↓H␈↓α␈↓ αX␈↓ βH␈↓ βx(GO A) ))


␈↓ ↓H␈↓α␈↓Now␈α∩that␈α∩assignment␈α⊃statements␈α∩are␈α∩out␈α∩in␈α⊃the␈α∩open␈α∩let's␈α⊃re-examine␈α∩"<=".␈α∩We␈α∩already␈α⊃know
␈↓ ↓H␈↓(page 129)␈αthat␈α"<="␈αdoes␈αmore␈αthan␈αsimply␈αassociate␈αthe␈αright␈αhand␈αside␈αwith␈αa␈αsymbol␈αtable␈αentry
␈↓ ↓H␈↓of␈α⊃the␈α⊂left␈α⊃hand␈α⊂side;␈α⊃it␈α⊂must␈α⊃also␈α⊂associate␈α⊃an␈α⊂environment␈α⊃with␈α⊂the␈α⊃function␈α⊂body,␈α⊃and␈α⊂this
␈↓ ↓H␈↓environment␈α⊗is␈α⊗to␈α⊗be␈α⊗used␈α⊗for␈α∃accessing␈α⊗non-local␈α⊗variables.␈α⊗This␈α⊗operation␈α⊗of␈α∃associating
␈↓ ↓H␈↓environments is called forming the ␈↓↓closure␈↓. We thus might be tempted to say:

␈↓ ↓H␈↓α␈↓ ∧Af <= λ[[ ... ] ...] ␈↓is␈↓α  f ← function[λ[[ ...] ...] ].

␈↓ ↓H␈↓Alas, this implementation is still not sufficient as we will see in Section 4.22.



␈↓ ↓H␈↓␈↓ ¬+␈↓↓Problems involving ␈↓αprog␈↓↓␈↓α


␈↓ ↓H␈↓I. Write ␈↓αprog␈↓-versions of the following functions (or predicates).

␈↓ ↓H␈↓␈↓↓1.␈↓α␈αmember[x;y]␈α
<=␈α...␈α:␈α
x␈↓␈αis␈αatomic;␈α
␈↓αy␈↓␈αis␈αa␈α
list␈αof␈α
atoms.␈α ␈↓αmember␈↓␈αis␈α
to␈αreturn␈α␈↓
t␈↓␈α
just␈αin␈αthe␈α
case␈αthat␈α␈↓αx␈↓␈α
is
␈↓ ↓H␈↓␈↓ α_one of the elements in ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓ The factorial function.

␈↓ ↓H␈↓␈↓↓3.␈↓α␈α
delete[x;y]␈α∞<=␈α
...␈α
:␈α∞x␈↓␈α
is␈α∞atomic;␈α
␈↓αy␈↓␈α
is␈α∞a␈α
list␈α∞of␈α
atoms.␈α
 ␈↓αdelete␈↓␈α∞is␈α
to␈α∞return␈α
a␈α
list␈α∞which␈α
looks␈α∞like␈α
␈↓αy␈↓,
␈↓ ↓H␈↓␈↓ α_except all occurrences of ␈↓αx␈↓ have been deleted.

␈↓ ↓H␈↓␈↓↓4.␈↓ The ␈↓αappend␈↓ function.

␈↓ ↓H␈↓␈↓↓5.␈↓α last[x] <= ... : x␈↓ is a non-empty list.  ␈↓αlast␈↓ is to return the last element in ␈↓αx␈↓.
␈↓ ↓H␈↓␈↓↓4.13␈↓ 	→The ␈↓αprog␈↓↓-feature     143␈↓α


␈↓ ↓H␈↓␈↓↓6.␈↓ Now write the S-expr translations of each of your functions.

␈↓ ↓H␈↓II. What is necessary to extend the evaluator to recognize ␈↓αprog␈↓ and friends?

␈↓ ↓H␈↓III.␈αThe␈α␈↓αgo[cdr[...]]␈↓-construct␈αon␈αpage 141␈αis␈αbetter␈αhandled␈αwith␈αa␈α␈↓↓case␈αstatement␈↓.␈αA␈αtypical␈αsyntax
␈↓ ↓H␈↓␈↓ α_for such might be:

␈↓ ↓H␈↓␈↓ ∧rcase<index>of <form␈↓β1␈↓>; ...<form␈↓βn␈↓>. 

␈↓ ↓H␈↓<index>␈α⊃is␈α⊃to␈α⊂evaluate␈α⊃to␈α⊃an␈α⊂integer,␈α⊃i.␈α⊃Where␈α⊂0<i≤n.␈α⊃The␈α⊃i␈↓πth␈↓␈α⊂<form>␈α⊃of␈α⊃the␈α⊃case-statement␈α⊂is
␈↓ ↓H␈↓␈↓ α_executed,␈α⊃and␈α⊃is␈α⊃the␈α⊃value␈α⊃of␈α∩the␈α⊃statement.␈α⊃ Construct␈α⊃a␈α⊃reasonable␈α⊃case␈α∩statement␈α⊃and
␈↓ ↓H␈↓␈↓ α_extend the evaluator to recognize it.

␈↓ ↓H␈↓IV. Some languages allow constructs like:
␈↓ ↓H␈↓␈↓ βP(␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x ␈↓↓else␈↓ y) ← exp, which is to mean the same as:

␈↓ ↓H␈↓␈↓ ∧|␈↓↓if␈↓ p(x) ␈↓↓then␈↓ x← exp ␈↓↓else␈↓ y ← exp

␈↓ ↓H␈↓Can such a construct be written in LISP?

␈↓ ↓H␈↓V.␈α⊂Compare␈α∂the␈α⊂␈↓αprog␈↓␈α∂version␈α⊂of␈α∂␈↓αlength␈↓␈α⊂on␈α⊂page 136␈α∂with␈α⊂␈↓αlength␈↓β1␈↓␈α∂on␈α⊂page 48.␈α∂Do␈α⊂you␈α⊂see␈α∂any
␈↓ ↓H␈↓␈↓ α_interesting relationships?



␈↓ ↓H␈↓␈↓ ¬'␈↓↓4.14  Alternatives to ␈↓αprog␈↓␈↓α


␈↓ ↓H␈↓The␈α⊃␈↓αprog␈↓␈α⊂feature␈α⊃of␈α⊂LISP␈α⊃is␈α⊃an␈α⊂effective␈α⊃means␈α⊂for␈α⊃encoding␈α⊂iterative␈α⊃algorithms,␈α⊃however␈α⊂it
␈↓ ↓H␈↓suffers␈α⊂from␈α⊂a␈α⊂few␈α⊂draw-backs.␈α⊂For␈α⊂example,␈α⊂The␈α⊂label-and-␈↓αgo␈↓␈α⊂style␈α⊂of␈α⊂control␈α⊂is␈α⊂only␈α⊃a␈α⊂slight
␈↓ ↓H␈↓modification␈α∞of␈α∞the␈α∞control␈α∞mechanisms␈α∞which␈α
are␈α∞typically␈α∞used␈α∞to␈α∞control␈α∞a␈α∞hardware␈α
machine,
␈↓ ↓H␈↓and␈α∩thus␈α∩the␈α∪level␈α∩of␈α∩description␈α∪which␈α∩is␈α∩required␈α∪tends␈α∩to␈α∩obscure␈α∪the␈α∩actual␈α∩flow␈α∪of␈α∩the
␈↓ ↓H␈↓algorithm␈α⊃unless␈α⊃the␈α⊃programmer␈α⊃is␈α⊃careful.␈α⊃A␈α⊃slight␈α⊃extension␈α⊃to␈α⊃conditional␈α⊃expressions␈α⊃and
␈↓ ↓H␈↓conditional␈α
statements␈α
can␈αalleviate␈α
some␈α
of␈αthe␈α
confusion␈α
which␈αis␈α
likely␈α
when␈αconstucting␈α
complex
␈↓ ↓H␈↓␈↓αprog␈↓s.

␈↓ ↓H␈↓As␈α∪conditionals␈α∩are␈α∪now␈α∩defined,␈α∪e␈↓βi␈↓␈α∩must␈α∪be␈α∩a␈α∪single␈α∩expression␈α∪to␈α∩be␈α∪evaluated.␈α∪ With␈α∩the
␈↓ ↓H␈↓introduction␈α∞of␈α∂side␈α∞effects,␈α∂it␈α∞is␈α∂convenient␈α∞to␈α∞extend␈α∂conditionals␈α∞to␈α∂include␈α∞components␈α∂of␈α∞the
␈↓ ↓H␈↓form:␈α∂p␈↓βi␈↓ → e␈↓βi1␈↓;␈α∂...␈α∂e␈↓βin␈↓.␈α∂ This␈α∂extended␈α∂component␈α∂is␈α⊂to␈α∂be␈α∂evaluated␈α∂as␈α∂follows:␈α∂if␈α∂p␈↓βi␈↓␈α∂is␈α⊂true,␈α∂then
␈↓ ↓H␈↓evaluate the e␈↓βij␈↓'s from left to right, with the value of the component to be the value of e␈↓βin␈↓␈↓π 76␈↓.

␈↓ ↓H␈↓For example, this feature, used in ␈↓αprog␈↓s would allow us to replace:

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 76␈↓␈α⊂This␈α⊃extended␈α⊂conditional␈α⊂expression␈α⊃is␈α⊂available␈α⊂on␈α⊃versions␈α⊂of␈α⊂LISP␈α⊃1.6␈α⊂on␈α⊃the␈α⊂PDP-10
␈↓ ↓H␈↓[Moo 76], [Qua xx], [Int 75].
␈↓ ↓H␈↓␈↓↓144  Evaluation␈↓ #4.14␈↓


␈↓ ↓H␈↓␈↓ αX␈↓ β_ ....
␈↓ ↓H␈↓␈↓ αX␈↓ β_[p␈↓β1␈↓ → ␈↓αgo[l]]
␈↓ ↓H␈↓α␈↓ αXm
␈↓ ↓H␈↓α␈↓ αX␈↓ β_ ...
␈↓ ↓H␈↓α␈↓ αX␈↓ β_return[␈↓
t␈↓α];
␈↓ ↓H␈↓α␈↓ αXl␈↓ β_␈↓e␈↓β1␈↓;
␈↓ ↓H␈↓␈↓ αX␈↓ β_e␈↓β2␈↓;
␈↓ ↓H␈↓␈↓ αX␈↓ β_ ...
␈↓ ↓H␈↓␈↓ αX␈↓ β_␈↓αgo[m];

␈↓ ↓H␈↓with:

␈↓ ↓H␈↓␈↓ ∧} ... [p␈↓β1␈↓ → e␈↓β1␈↓;e␈↓β2␈↓; ... ]   ...; ␈↓αreturn[␈↓
t␈↓α]].

␈↓ ↓H␈↓The␈α∞improved␈α∞readibility␈α∞is␈α∞largely␈α∞do␈α∞to␈α∂the␈α∞localizing␈α∞or␈α∞"packaging"␈α∞of␈α∞the␈α∞actions␈α∂with␈α∞their
␈↓ ↓H␈↓initiators;␈αwe␈αneed␈αnot␈αscan␈αan␈αarbitrarily␈αlong␈αpiece␈αof␈αtext␈αto␈αdiscover␈αwhat␈αthe␈αcomputation␈αwill
␈↓ ↓H␈↓be when the predicate is true.

␈↓ ↓H␈↓Several␈α
languages␈αhave␈α
included␈αmore␈α
"packaged"␈α
versions␈αof␈α
iterative␈αcontrol.␈α
The␈αmotivation␈α
was
␈↓ ↓H␈↓similar␈α∂to␈α∂that␈α∂which␈α∂we␈α∂used␈α∂in␈α∂justifying␈α∂recursive␈α∂control:␈α∂we␈α∂didn't␈α∂care␈α∂␈↓↓how␈↓␈α∂recursion␈α∞was
␈↓ ↓H␈↓implemented, all we wished to discuss was the ␈↓↓effect␈↓ or ␈↓↓behavior␈↓ of recursion␈↓π 77␈↓.

␈↓ ↓H␈↓Clearly␈α∃an␈α∃iterative␈α∃unit␈α∃must␈α∃allow␈α∃the␈α∃programmer␈α∃a␈α∃reasonable␈α∃degree␈α∃of␈α∃freedom␈α∃and
␈↓ ↓H␈↓naturalness␈α∂in␈α∂expression.␈α∂What␈α∂should␈α∂also␈α∂be␈α∂recognized␈α∂is␈α∂that␈α∂the␈α∂structural␈α∂unit␈α∂should␈α∞be
␈↓ ↓H␈↓amenable␈α∂to␈α∞analysis␈α∂to␈α∞the␈α∂same␈α∞degree␈α∂as␈α∞that␈α∂allowed␈α∞in␈α∂recursion.␈α∞We␈α∂must␈α∞be␈α∂able␈α∂to␈α∞state
␈↓ ↓H␈↓precise␈α∩properties␈α∩of␈α∩algorithms␈α∪which␈α∩use␈α∩these␈α∩constructs,␈α∩and␈α∪we␈α∩should␈α∩be␈α∩able␈α∪to␈α∩prove
␈↓ ↓H␈↓properties␈αof␈α
such␈αalgorithms.␈α With␈α
the␈αcontrol␈αof␈α
the␈αloop␈αstructure␈α
in␈αthe␈αlanguage␈α
rather␈αthan
␈↓ ↓H␈↓in␈αthe␈α
hands␈αof␈αthe␈α
programmer,␈αthe␈α
static␈αtext␈αand␈α
the␈αdynamic␈α
flow␈αof␈αthe␈α
execution␈αhave␈αa␈α
close
␈↓ ↓H␈↓relationship.␈α
General␈αuse␈α
of␈αlabel-and-␈↓αgo␈↓'s␈α
and␈α
assignments␈αdoes␈α
not␈αmaintain␈α
such␈αproperties.␈α
 Our
␈↓ ↓H␈↓iterative␈α
control␈α∞construct␈α
should␈α
therefore␈α∞capture␈α
all␈α∞of␈α
the␈α
essential␈α∞ingredients␈α
of␈α∞an␈α
iteration,
␈↓ ↓H␈↓and its semantics should be restricted such that its static text does indeed reflect its dynamic flow.

␈↓ ↓H␈↓Our␈αfirst␈α
example␈αis␈αbased␈α
on␈αthe␈α
MacLISP␈α␈↓αdo␈↓␈α[Moo 76].␈α
With␈αsome␈α
inessential␈αchanges,␈αits␈α
syntax
␈↓ ↓H␈↓is:

␈↓ ↓H␈↓␈↓ ∧h␈↓αdo␈↓ ¬_[␈↓<var␈↓β1␈↓> <init␈↓β1␈↓> <step␈↓β1␈↓>;
␈↓ ↓H␈↓␈↓ ∧h␈↓ ¬_ <var␈↓β2␈↓> <init␈↓β2␈↓> <step␈↓β2␈↓>;
␈↓ ↓H␈↓␈↓ ∧h␈↓ ¬_      ...
␈↓ ↓H␈↓␈↓ ∧h␈↓ ¬_ <var␈↓βn␈↓> <init␈↓βn␈↓> <step␈↓βn␈↓>;
␈↓ ↓H␈↓␈↓ ∧h␈↓ ¬_[<pred> → <exit>]
␈↓ ↓H␈↓␈↓ ∧h␈↓ ¬_ <body> ]


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 77␈↓␈α↔Indeed␈α⊗we␈α↔␈↓↓could␈↓␈α⊗have␈α↔replaced␈α⊗recursive␈α↔control␈α⊗with␈α↔an␈α⊗appropriate␈α↔combination␈α⊗of
␈↓ ↓H␈↓label-and-␈↓αgo␈↓'s.
␈↓ ↓H␈↓␈↓↓4.14␈↓ λpAlternatives to ␈↓αprog␈↓     145␈↓α


␈↓ ↓H␈↓The␈αconstruct␈α
captures␈αthe␈α
ideas␈αof␈α
intialization␈αand␈αupdating␈α
of␈αvariables␈α
nicely.␈α Each␈α
<var␈↓βi␈↓>␈αis
␈↓ ↓H␈↓initialized␈αto␈αits␈α<init␈↓βi␈↓>-value␈αsimultaneously.␈α Each␈α<step␈↓βi␈↓>␈αis␈αa␈αform␈αwhich␈αwill␈αbe␈αevaluated␈αupon
␈↓ ↓H␈↓proper␈αcompletion␈αof␈αeach␈αloop␈αof␈αthe␈α␈↓αdo␈↓.␈αThe␈α<pred>␈αis␈αevaluated,␈αand␈αon␈αgiving␈αvalue␈α␈↓
t␈↓␈αthe␈αloop
␈↓ ↓H␈↓will␈α∂terminate,␈α⊂returning␈α∂the␈α⊂value␈α∂of␈α∂<exit>.␈α⊂ If␈α∂<pred>␈α⊂gives␈α∂␈↓
f␈↓␈α∂then␈α⊂<body>␈α∂is␈α⊂executed.␈α∂This
␈↓ ↓H␈↓component␈α∞of␈α∂the␈α∞␈↓αdo␈↓␈α∞is␈α∂a␈α∞␈↓αprog␈↓␈α∞body;␈α∂when␈α∞the␈α∞last␈α∂statement␈α∞in␈α∞<body>␈α∂is␈α∞executed,␈α∂the␈α∞<step␈↓βi␈↓>
␈↓ ↓H␈↓forms are evaluated and assigned to the <var␈↓βi␈↓>'s, and another cycle of the ␈↓αdo␈↓ is begun.

␈↓ ↓H␈↓Since␈α
the␈α<body>␈α
of␈α
the␈α␈↓αdo␈↓␈α
is␈αa␈α
␈↓αprog␈↓␈α
body,␈αthe␈α
␈↓αreturn␈↓␈αstatement␈α
may␈α
appear.␈αThis␈α
is␈αan␈α
undesirable
␈↓ ↓H␈↓feature␈αsince␈α
the␈αdynamic␈α
flow␈αwill␈α
then␈αdiverge␈α
from␈αthe␈α
static␈αtext.␈α
But␈αconsider␈α
the␈α␈↓αdo␈↓␈αversion␈α
of
␈↓ ↓H␈↓␈↓αmember␈↓:
␈↓ ↓H␈↓α␈↓ αxmember <= λ[a;l]␈↓ ∧hdo␈↓ ¬_[x l rest[x];
␈↓ ↓H␈↓α␈↓ αx␈↓ ∧h␈↓ ¬_ [null[x] → ␈↓
f␈↓α]
␈↓ ↓H␈↓α␈↓ αx␈↓ ∧h␈↓ ¬_ [eq[first[x];a] → return[␈↓
t␈↓α]]  ]

␈↓ ↓H␈↓This␈α∩algorithm␈α∩could␈α∩be␈α∩expressed␈α∩without␈α∩␈↓αreturn␈↓␈α∩but␈α∩the␈α∩resulting␈α∩program␈α∩is␈α∩unnecessarily
␈↓ ↓H␈↓complex.

␈↓ ↓H␈↓An alternative iterative construct was proposed in [Wis 75].

␈↓ ↓H␈↓α␈↓ β+repeat[␈↓<st-list␈↓β1␈↓>;␈↓αwhile ␈↓<pred␈↓β1␈↓>; <st-list␈↓β2␈↓>; ␈↓αuntil ␈↓<pred␈↓β2␈↓>; <st-list␈↓β3␈↓>]

␈↓ ↓H␈↓where␈α
<pred␈↓βi␈↓>␈αis␈α
a␈αpredicate,␈α
and␈α<st-list␈↓βi␈↓>␈α
is␈αa␈α
list␈αof␈α
statements.␈αThe␈α
list␈αmay␈α
be␈αempty,␈α
but␈αmay
␈↓ ↓H␈↓not contain ␈↓αreturn␈↓s or ␈↓αgo␈↓s.

␈↓ ↓H␈↓The␈α
semantics␈αis␈α
as␈αfollows:␈α
<st-list␈↓β1␈↓>␈αis␈α
executed;␈α<pred␈↓β1␈↓>␈α
is␈αthen␈α
evaluated␈αand␈α
if␈αfalse␈α
we␈αexit␈α
the
␈↓ ↓H␈↓␈↓αrepeat␈↓␈αwith␈α␈↓
f␈↓.␈αIf␈α<pred␈↓β1␈↓>␈αis␈αtrue,␈αthen␈αwe␈αexecute␈α<st-list␈↓β2␈↓>␈αand␈αtest␈α<pred␈↓β2␈↓>;␈αif␈α<pred␈↓β2␈↓>␈αis␈αtrue␈αwe
␈↓ ↓H␈↓exit with ␈↓
t␈↓, otherwise we execute <st-list␈↓β3␈↓> and iterate the loop beginning again at <st-list␈↓β1␈↓>.

␈↓ ↓H␈↓For example we could write ␈↓αmember␈↓ as:

␈↓ ↓H␈↓α␈↓ αXmember <= λ[[a;l]
␈↓ ↓H␈↓α␈↓ αX␈↓ βxrepeat[while [not[null[l]]; until [equal[a;first[l]];l ← rest[l] ]

␈↓ ↓H␈↓The␈αdifficulty␈α
which␈αwe␈α
encountered␈αwith␈α
the␈αMacLISP␈α
␈↓αdo␈↓␈αhas␈α
been␈αalleviated,␈α
however␈αthe␈α
␈↓αrepeat␈↓
␈↓ ↓H␈↓construct␈α
has␈α
several␈α
shortcomings␈α
of␈α
its␈α
own.␈α
In␈α
particular,␈α
we␈α
have␈α
no␈α
means␈α
for␈α
designating␈α
what
␈↓ ↓H␈↓variables␈α
are␈α
to␈α
be␈αinitialized␈α
and␈α
incremented␈α
within␈α
the␈αloop.␈α
 Such␈α
variables␈α
must␈α
be␈αdeclared
␈↓ ↓H␈↓and␈α
initialized␈α
external␈α
to␈α
the␈α
␈↓αrepeat␈↓;␈α
also␈α
the␈α
stepping␈α
of␈α
the␈α
loop␈α
variables␈α
must␈α
be␈α∞done␈α
using
␈↓ ↓H␈↓the␈α∞assignment␈α
statement.␈α∞ Similarly␈α
the␈α∞power␈α∞of␈α
expression␈α∞on␈α
leaving␈α∞the␈α
␈↓αrepeat␈↓␈α∞is␈α∞limited;␈α
we
␈↓ ↓H␈↓cannot␈α⊂explicitly␈α⊂declare␈α⊂what␈α⊂values␈α⊂are␈α⊂to␈α⊂be␈α⊂returned.␈α⊂The␈α⊂value␈α⊂is␈α⊂that␈α⊂of␈α⊂the␈α∂appropriate
␈↓ ↓H␈↓<pred␈↓βi␈↓>.

␈↓ ↓H␈↓add do␈↓βjra␈↓
␈↓ ↓H␈↓␈↓↓146  Evaluation␈↓ #4.14␈↓


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.␈α
Some␈α
of␈α
the␈α
generality␈α
of␈α
␈↓αprog␈↓s␈α
can␈α
be␈αcontrolled␈α
by␈α
the␈α
use␈α
of␈α
a␈α
new␈α
control␈α
structure␈α
for␈αlist
␈↓ ↓H␈↓operations.␈αThe␈αconstruct␈α
is␈αcalled␈α␈↓αlit␈↓␈↓π 78␈↓.␈α
 ␈↓αlit␈↓␈αtakes␈αthree␈αarguments:␈α
a␈αbinary␈αfunction␈α
␈↓αf␈↓,␈αa␈αlist␈α␈↓αl␈↓,␈α
and
␈↓ ↓H␈↓a␈α
value␈α
␈↓αv␈↓.␈α
 If␈α␈↓αl␈↓␈α
is␈α
empty,␈α
give␈α␈↓αv␈↓;␈α
otherwise␈α
apply␈α
␈↓αf␈↓␈α
to␈αthe␈α
first␈α
element␈α
of␈α␈↓αl␈↓␈α
and␈α
the␈α
effect␈αof␈α
applying
␈↓ ↓H␈↓␈↓αlit␈↓ to the remainder of ␈↓αl␈↓.

␈↓ ↓H␈↓For example ␈↓αappend␈↓ could be expressed as:
␈↓ ↓H␈↓α␈↓ ∧Cappend <= λ[[x;y]lit[function[concat];x;y]]

␈↓ ↓H␈↓Give a non-␈↓αprog␈↓ definition for ␈↓αlit␈↓.

␈↓ ↓H␈↓II.␈α
Here␈α
is␈α
another␈αuseful␈α
extension␈α
to␈α
LISP:␈αInstead␈α
of␈α
requiring␈α
that␈αthe␈α
body␈α
of␈α
a␈αλ-definition␈α
be
␈↓ ↓H␈↓a␈α∞single␈α
expression:␈α∞␈↓λx␈↓␈α
in␈α∞␈↓αλ[[ ... ]␈↓λx␈↓α]␈↓,␈α
allow␈α∞bodies␈α
of␈α∞the␈α
form:␈α∞␈↓λx␈↓β1␈↓α; ...; ␈↓λx␈↓βn␈↓α␈↓,␈α
giving␈α∞rise␈α∞to␈α
λ-definitions
␈↓ ↓H␈↓like␈α∂␈↓αλ[[ ... ]␈↓λx␈↓β1␈↓α; ...; ␈↓λx␈↓βn␈↓α]␈↓.␈α∂The␈α∂application␈α∞of␈α∂such␈α∂a␈α∂definition␈α∞means:␈α∂bind␈α∂the␈α∂λ-variables␈α∂as␈α∞usual,
␈↓ ↓H␈↓then evaluate the ␈↓λx␈↓βi␈↓'s from left to right returning as value, ␈↓λx␈↓βn␈↓.

␈↓ ↓H␈↓Extend the evaluator of Section 4.5 to handle such constructs.

␈↓ ↓H␈↓III. Give an S-expr representation for the ␈↓αrepeat␈↓ expression and add ␈↓αrepeat␈↓ to ␈↓αeval␈↓ of Section 4.5.



␈↓ ↓H␈↓␈↓ ¬3␈↓↓4.15  Extensions to ␈↓αeval␈↓␈↓α


␈↓ ↓H␈↓The␈αintroduction␈αof␈αthe␈α␈↓αprog␈↓-feature␈αcompletes␈αour␈αsyntactic␈αdescription␈αof␈αthe␈αlanguage␈αconstructs
␈↓ ↓H␈↓of␈αLISP.␈αWe␈αwould␈αlike␈αto␈αgive␈αa␈αnew␈αversion␈αof␈α␈↓αeval␈↓␈αwhich␈αdescribes␈αthe␈αsemantics␈αof␈α␈↓αprog␈↓s␈αin␈αa
␈↓ ↓H␈↓manner␈αwhich␈α
accurately␈αreflects␈α
the␈αtechniques␈α
used␈αin␈α
implementations.␈αWe␈α
could␈αsimply␈α
simulate
␈↓ ↓H␈↓␈↓αprog␈↓␈α∪behavior␈α∪using␈α∪recursive␈α∪techniques,␈α∪but␈α∪the␈α∪iterative␈α∪control␈α∪expressed␈α∪in␈α∪␈↓αprog␈↓s␈α∪is␈α∪an
␈↓ ↓H␈↓important␈α∞idea␈α∞in␈α∂its␈α∞own␈α∞right␈α∞and␈α∂is␈α∞a␈α∞simple␈α∞instance␈α∂of␈α∞non-recursive␈α∞control.␈α∂A␈α∞mechanism
␈↓ ↓H␈↓which␈α
faithfully␈αimplements␈α
such␈α
control␈αstructures␈α
leads␈αeasily␈α
to␈α
the␈αidea␈α
of␈α
␈↓↓generalized␈αcontrol
␈↓ ↓H␈↓↓structures␈↓.

␈↓ ↓H␈↓The␈α⊂second␈α⊂interesting␈α⊃feature␈α⊂introduced␈α⊂with␈α⊂␈↓αprog␈↓s␈α⊃was␈α⊂the␈α⊂assignment␈α⊂statement.␈α⊃Again,␈α⊂we
␈↓ ↓H␈↓could␈αmirror␈α
most␈αof␈α
the␈αbehavior␈α
of␈αassignments␈αby␈α
careful␈αuse␈α
of␈αthe␈α
techniques␈αof␈αrecursion␈α
and
␈↓ ↓H␈↓symbol␈αtables,␈αbut␈αsuch␈αmodelling␈αwould␈αnot␈αadequately␈αreflect␈αthe␈αintent␈αor␈αindeed␈αthe␈αtechniques
␈↓ ↓H␈↓used␈α∩in␈α⊃implementing␈α∩such␈α⊃constructs.␈α∩We␈α⊃could␈α∩describe␈α⊃such␈α∩implementations␈α⊃in␈α∩a␈α⊃low-level
␈↓ ↓H␈↓machine␈α⊃language,␈α⊂but␈α⊃such␈α⊂practice␈α⊃would␈α⊃only␈α⊂introduce␈α⊃unnecessary␈α⊂details.␈α⊃Rather,␈α⊃we␈α⊂will
␈↓ ↓H␈↓describe␈α∞an␈α
evaluator␈α∞in␈α
LISP␈α∞using␈α
the␈α∞techniques␈α
we␈α∞have␈α
been␈α∞developing.␈α
In␈α∞the␈α∞process␈α
we
␈↓ ↓H␈↓will␈α∂elucidate␈α∂much␈α⊂more␈α∂than␈α∂just␈α∂␈↓αprog␈↓s␈α⊂and␈α∂assignments;␈α∂we␈α∂will␈α⊂lay␈α∂bare␈α∂much␈α∂more␈α⊂of␈α∂the
␈↓ ↓H␈↓behavior␈α∂which␈α∞was␈α∂implicit␈α∞in␈α∂the␈α∞previous␈α∂evaluators.␈α∞Those␈α∂evaluators␈α∞used␈α∂recursion␈α∂in␈α∞the

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 78␈↓ for ␈↓αl␈↓ist ␈↓αit␈↓erator [Bar 66]
␈↓ ↓H␈↓␈↓↓4.15␈↓ 	εExtensions to ␈↓αeval␈↓     147␈↓α


␈↓ ↓H␈↓explication␈α
of␈α
recursion,␈α
frequently␈α
depended␈α
on␈α
call-by-value␈α
in␈α
the␈α
explanation␈α∞of␈α
call-by-value,
␈↓ ↓H␈↓and␈αsuppressed␈αmuch␈αof␈αthe␈αdetail␈αof␈αbinding␈αand␈αlook␈αup.␈αThe␈αWeizenbaum␈αenvironments␈αadded
␈↓ ↓H␈↓more␈αdetail,␈αbut␈αfailed␈αto␈αdescribe␈αan␈αexplicit␈αmechanism␈αfor␈αthe␈αhandling␈αof␈αpartial␈αcomputations,
␈↓ ↓H␈↓neither␈α⊂showing␈α⊂where␈α⊂partial␈α⊃results␈α⊂were␈α⊂maintained␈α⊂or␈α⊃how␈α⊂the␈α⊂evaluator␈α⊂was␈α⊃to␈α⊂remember
␈↓ ↓H␈↓where␈α∞it␈α∞was␈α∂in␈α∞an␈α∞expression␈α∂when␈α∞it␈α∞had␈α∞to␈α∂evaluate␈α∞a␈α∞sub-expression.␈α∂ All␈α∞of␈α∞this␈α∂detail␈α∞will
␈↓ ↓H␈↓come␈α
out␈α
in␈α
the␈α
new␈α
evaluator.␈α
Since␈α
the␈α
structure␈α
of␈α
the␈α
new␈α
␈↓αeval␈↓␈α
is␈α
quite␈α
different␈α
from␈αthose␈α
seen
␈↓ ↓H␈↓before, and since the level of detail is more intense, we will proceed in several steps.

␈↓ ↓H␈↓First␈α∩we␈α∩discuss␈α∩some␈α∩generalizations␈α∩of␈α∩the␈α∩label-and-␈↓αgo␈↓␈α∩control␈α∩structures.␈α∩These␈α∩ideas␈α∩have
␈↓ ↓H␈↓importance␈α
in␈α
their␈α
own␈α
right␈α
when␈α
we␈α
discuss␈α
actual␈α
interactive␈α
implementations␈α
of␈α
LISP.␈αNext␈α
we
␈↓ ↓H␈↓develop␈α∂an␈α∂␈↓αeval␈↓␈α∂in␈α∂which␈α∂the␈α∂handling␈α∞of␈α∂access␈α∂structures␈α∂is␈α∂explicit.␈α∂ The␈α∂innovations␈α∂in␈α∞this
␈↓ ↓H␈↓evaluator␈α
will␈α
form␈α
the␈α
basic␈α
blocks␈α
which␈αwe␈α
will␈α
use␈α
to␈α
model␈α
parameter␈α
passing␈αand␈α
assignments.
␈↓ ↓H␈↓This␈αevaluator␈αwill␈αstill␈αbe␈αrecursively␈αdescribed,␈αand␈αwill␈αnot␈αhandle␈αthe␈α␈↓αprog␈↓␈αfeature.␈αIn␈αthe␈αfinal
␈↓ ↓H␈↓step␈α∂we␈α∂replace␈α⊂the␈α∂recursion␈α∂with␈α∂explicit␈α⊂control␈α∂and␈α∂with␈α∂this␈α⊂change␈α∂we␈α∂have␈α∂the␈α⊂basis␈α∂for
␈↓ ↓H␈↓adequate␈αtreatment␈αof␈αnon-recursive␈αcontrol.␈α Finally␈αwe␈αpresent␈αan␈αevaluator␈αwhich␈αhandles␈αall␈αof
␈↓ ↓H␈↓the ␈↓αprog␈↓-related constructs.



␈↓ ↓H␈↓␈↓ ∧W␈↓↓4.16  Non-recursive control structures␈↓


␈↓ ↓H␈↓On␈α
page 138␈α
we␈αdiscussed␈α
the␈α
␈↓αgo␈↓␈α
construct.␈αIn␈α
that␈α
discussion␈αwe␈α
noted␈α
that␈α
the␈αscope␈α
of␈α
the␈α␈↓αgo␈↓␈α
was
␈↓ ↓H␈↓not␈α⊂restricted␈α⊃to␈α⊂the␈α⊃current␈α⊂␈↓αprog␈↓;␈α⊂we␈α⊃need␈α⊂only␈α⊃locate␈α⊂an␈α⊂appropriate␈α⊃label␈α⊂in␈α⊃a␈α⊂␈↓↓dynamically␈↓
␈↓ ↓H␈↓surrounding␈α∃expression.␈α∀Thus␈α∃we␈α∀could␈α∃jump␈α∀out␈α∃of␈α∀an␈α∃expression,␈α∀passing␈α∃through␈α∀many
␈↓ ↓H␈↓intervening␈α∪expressions,␈α∪whereas␈α∀strict␈α∪recursive␈α∪control␈α∪requires␈α∀that␈α∪we␈α∪exit␈α∪functions␈α∀in␈α∪a
␈↓ ↓H␈↓level-by-level␈αfashion.␈α This␈αability␈α
to␈αexit␈αacross␈αmany␈α
levels␈αof␈αcomputation␈αfinds␈α
applications␈αat
␈↓ ↓H␈↓the␈αsystem␈αlevel␈α
in␈αinteractive␈αLISP␈αimplementations␈α
and␈αis␈αalso␈α
a␈αuseful␈αprogramming␈αfeature.␈α
For
␈↓ ↓H␈↓example,␈αif␈αsome␈αextraordinary␈αcondition␈αoccurs␈αwithin␈αa␈αcomputation␈αwe␈αmight␈αwish␈αto␈αabort␈αthat
␈↓ ↓H␈↓whole␈α∞endeavor.␈α∂As␈α∞things␈α∂currently␈α∞stand␈α∂we␈α∞would␈α∂have␈α∞to␈α∂supply␈α∞an␈α∂additional␈α∞value␈α∂in␈α∞the
␈↓ ↓H␈↓range␈α
of␈α
each␈α
function␈α
which␈α
could␈α
occur␈α∞in␈α
that␈α
computation.␈α
Each␈α
function␈α
would␈α
have␈α∞to␈α
test
␈↓ ↓H␈↓for␈αthat␈αexception-value␈αand␈αwhen␈αit␈αis␈αfound,␈αreturn␈αthat␈αvalue␈αto␈αthe␈αcaller.␈αThis␈αis␈αan␈αeffective,
␈↓ ↓H␈↓but␈α
not␈αelegant,␈α
solution␈αto␈α
the␈αproblem.␈α
Notice␈αthat␈α
this␈αis␈α
the␈αsolution␈α
posed␈αin␈α
our␈αuse␈α
of␈α
␈↓λB␈↓␈αin
␈↓ ↓H␈↓conjunction␈αwith␈αstrict␈αfunctions.␈α Indeed,␈αa␈αmore␈αelegant␈αsolution␈αhas␈αits␈αorigins␈αin␈αthe␈αearly␈αLISP
␈↓ ↓H␈↓debugging␈α⊂tools.␈α⊂If␈α∂a␈α⊂computation␈α⊂produced␈α∂an␈α⊂error␈α⊂detectable␈α∂as␈α⊂␈↓λB␈↓,␈α⊂then␈α∂it␈α⊂typically␈α⊂was␈α∂the
␈↓ ↓H␈↓responsibility␈α
of␈αthe␈α
LISP␈α
controller␈αto␈α
print␈α
an␈αerror␈α
message␈α
and␈αterminate␈α
the␈αcomputation.␈α
Such
␈↓ ↓H␈↓behavior␈α
was␈αacceptable␈α
for␈αsimple␈α
computations.␈α
As␈αcomputations␈α
became␈αmore␈α
complex␈αit␈α
became
␈↓ ↓H␈↓clear␈α∀that␈α∃the␈α∀occurrence␈α∃of␈α∀one␈α∃error␈α∀need␈α∀not␈α∃signal␈α∀the␈α∃termination␈α∀of␈α∃␈↓↓all␈↓␈α∀computation.
␈↓ ↓H␈↓Particularly␈α~since␈α→the␈α~expressions␈α→were␈α~available␈α→as␈α~data␈α→structures,␈α~the␈α~opportunity␈α→for
␈↓ ↓H␈↓self-correcting␈α∩programs␈α∩existed␈α∩in␈α∩LISP.␈α∩Thus␈α∩LISP␈α∩needed␈α∩a␈α∩mechanism␈α∩for␈α∩more␈α∩selective
␈↓ ↓H␈↓control of error messages.

␈↓ ↓H␈↓The␈α∞early␈α∞LISP␈α∞systems␈α∞supplied␈α∞a␈α∞pair␈α∞of␈α∞functions␈α∞named␈α∞␈↓αerrset␈↓␈α∞and␈α∞␈↓αerr␈↓.␈α∞ The␈α∞function␈α∞␈↓αerrset␈↓
␈↓ ↓H␈↓evaluates␈αits␈αfirst␈αargument␈αin␈αthe␈αcurrent␈αcontext.␈α If␈αno␈αerror␈αoccurs␈αin␈αthat␈αevaluation,␈αthe␈αresult
␈↓ ↓H␈↓is␈α
␈↓αconcat␈↓ed␈α
onto␈α( )␈α
and␈α
returned.␈αIf␈α
an␈α
error␈αdoes␈α
occur␈α
then␈α
the␈αvalue␈α
of␈α
the␈α␈↓αerrset␈↓␈α
is␈α
␈↓
f␈↓.␈αNotice␈α
that
␈↓ ↓H␈↓␈↓↓148  Evaluation␈↓ &4.16␈↓


␈↓ ↓H␈↓in␈α
either␈αcase␈α
the␈α
␈↓αerrset␈↓␈αterminates.␈α
We␈α
can␈αtest␈α
the␈α
success␈αof␈α
our␈α
calculation␈αby␈α
sampling␈αthe␈α
value
␈↓ ↓H␈↓of ␈↓αerrset␈↓: ␈↓
f␈↓ implies failure; otherwise the ␈↓αfirst␈↓ element of the result is the true value.

␈↓ ↓H␈↓The␈αuser␈αcan␈αalso␈αforce␈α
the␈αoccurrence␈αof␈αan␈α"error"␈αby␈α
calling␈α␈↓αerr␈↓.␈α The␈αunary␈αfunction␈α␈↓αerr␈↓␈α
returns
␈↓ ↓H␈↓the␈αvalue␈α
of␈αits␈αargument␈α
to␈αthe␈α
dynamically␈αenclosing␈α␈↓αerrset␈↓␈α
or,␈αif␈α
there␈αis␈αno␈α
such␈α␈↓αerrset␈↓,␈αthe␈α
value
␈↓ ↓H␈↓is␈α⊂returned␈α∂as␈α⊂the␈α∂final␈α⊂value␈α∂of␈α⊂the␈α⊂computation.␈α∂For␈α⊂example␈α∂if␈α⊂␈↓αerr␈↓␈α∂is␈α⊂restricted␈α⊂to␈α∂returning
␈↓ ↓H␈↓values␈α
in␈α
a␈α
set␈αdisjoint␈α
from␈α
those␈α
returned␈αby␈α
a␈α
non-"error"␈α
computation,␈αthen␈α
the␈α
user␈α
can␈αtest␈α
the
␈↓ ↓H␈↓value of ␈↓αerrset␈↓ to discover the type of "error".

␈↓ ↓H␈↓The␈α
freedom␈α∞allowed␈α
by␈α∞the␈α
␈↓αerrset-err␈↓␈α∞combination␈α
soon␈α∞became␈α
exploited␈α∞in␈α
ways␈α∞not␈α
originally
␈↓ ↓H␈↓intended.␈α
The␈α
use␈α
of␈α∞␈↓αerrset␈↓␈α
and␈α
␈↓αerr␈↓␈α
in␈α∞non-error-handling␈α
contexts␈α
often␈α
became␈α∞quite␈α
confusing.
␈↓ ↓H␈↓The␈αMacLISP␈α[Moo 76]␈αdialect␈αincludes␈αa␈αpair␈αof␈αconstructs␈αnamed␈α␈↓αcatch␈↓␈αand␈α␈↓αthrow␈↓␈αto␈αbe␈αused␈αin
␈↓ ↓H␈↓these situations.

␈↓ ↓H␈↓␈↓αcatch␈↓␈α⊃and␈α⊃␈↓αthrow␈↓␈α⊃are␈α∩both␈α⊃binary␈α⊃functions.␈α⊃Both␈α∩first␈α⊃arguments␈α⊃are␈α⊃expressions;␈α∩both␈α⊃second
␈↓ ↓H␈↓arguments are interpreted like ␈↓αprog␈↓ labels.

␈↓ ↓H␈↓␈↓αcatch␈↓[<form>;<label>]␈α∂evaluates␈α∂its␈α∂first␈α∞argument␈α∂in␈α∂the␈α∂current␈α∞context,␈α∂and␈α∂returns␈α∂that␈α∞value,
␈↓ ↓H␈↓except␈α
that␈α
if␈αduring␈α
that␈α
evaluation,␈αa␈α
␈↓αthrow[␈↓<form>;<label>]␈α
with␈αthe␈α
same␈α
<label>␈α
is␈αevaluated,
␈↓ ↓H␈↓then the value of ␈↓αthrow␈↓'s <form> is returned immediately.

␈↓ ↓H␈↓For example␈↓π 79␈↓:
␈↓ ↓H␈↓α␈↓ βxcatch[␈↓ ∧Hmapfirst[␈↓ ¬Hfunction[λ[[x][x<0 → throw[x;negative];␈↓
t␈↓α → f[x]]]];
␈↓ ↓H␈↓α␈↓ βx␈↓ ∧H␈↓ ¬Hy];
␈↓ ↓H␈↓α␈↓ βx␈↓ ∧Hnegative]

␈↓ ↓H␈↓Assuming␈α␈↓αy␈↓␈αis␈αa␈αlist␈αof␈αnumbers,␈αthis␈αexpression␈αwill␈αreturn␈αa␈αlist␈αof␈α␈↓αf␈↓␈αapplied␈αto␈αeach␈αelement␈αof␈α␈↓αy␈↓
␈↓ ↓H␈↓if each element of ␈↓αy␈↓ is non-negative, otherwise it will return the first negative element of ␈↓αy␈↓.



␈↓ ↓H␈↓␈↓ ¬∀␈↓↓4.17  ␈↓αeval␈↓ with explicit access␈↓α


␈↓ ↓H␈↓There␈α⊃are␈α⊃two␈α⊃major␈α⊃portions␈α⊃of␈α⊃the␈α⊃evaluation␈α⊃schemes␈α⊃which␈α⊃should␈α⊃be␈α⊃subjected␈α⊃to␈α⊃closer
␈↓ ↓H␈↓scrutiny␈α
before␈α∞we␈α
attempt␈α
to␈α∞discuss␈α
implementations:␈α∞the␈α
access␈α
and␈α∞binding␈α
structures,␈α∞and␈α
the
␈↓ ↓H␈↓description of recursive control.  This section will look at access and binding.

␈↓ ↓H␈↓The␈αWeizenbaum␈αenvironments␈αgive␈αa␈αnice␈αgraphical␈αrepresentation␈αof␈αthe␈αaccess␈αstructures,␈αbut␈αit
␈↓ ↓H␈↓would␈α∞be␈α∂instructive␈α∞to␈α∞express␈α∂these␈α∞ideas␈α∂in␈α∞terms␈α∞of␈α∂LISP␈α∞functions.␈α∞ This␈α∂would␈α∞give␈α∂us␈α∞an
␈↓ ↓H␈↓algorithm,␈α∂suitable␈α⊂for␈α∂implementation,␈α⊂and␈α∂would␈α⊂describe␈α∂the␈α⊂mechanisms␈α∂of␈α⊂LISP␈α∂at␈α⊂a␈α∂more
␈↓ ↓H␈↓detailed␈αlevel␈α
than␈αthat␈α
in␈αthe␈α
evaluators␈αof␈α
Section 4.5.␈αThe␈α
description␈αwe␈α
will␈αgive␈α
will␈αinvolve
␈↓ ↓H␈↓primitive␈αnotions␈αjust␈αas␈αthe␈αprior␈α␈↓αeval␈↓'s␈αdo,␈αhowever␈αthe␈αlevel␈αof␈αdetail␈αwhich␈αthey␈αcapture␈αwill␈αbe

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 79␈↓ [Moo 76]
␈↓ ↓H␈↓␈↓↓4.17␈↓ λH␈↓αeval␈↓ with explicit access     149␈↓α


␈↓ ↓H␈↓more␈α∀readily␈α∃transcribed␈α∀to␈α∀implementation␈α∃devices.␈α∀ As␈α∀we␈α∃have␈α∀previously␈α∃mentioned,␈α∀the
␈↓ ↓H␈↓Weizenbaum␈α
environments␈α
leave␈α
much␈α
of␈α
the␈α
detail␈αof␈α
access␈α
and␈α
binding␈α
implicit.␈α
It␈α
will␈α
be␈αa␈α
goal
␈↓ ↓H␈↓of this section to fill in these details.

␈↓ ↓H␈↓Recall␈α
that␈α
a␈α∞Weizenbaum␈α
environment␈α
was␈α∞created␈α
at␈α
function-entry␈α∞time.␈α
 As␈α
we␈α∞evaluated␈α
the
␈↓ ↓H␈↓arguments␈α⊃to␈α⊃a␈α⊂function,␈α⊃we␈α⊃saved␈α⊃the␈α⊂results␈α⊃in␈α⊃some␈α⊃fashion␈α⊂and,␈α⊃when␈α⊃all␈α⊃arguments␈α⊂were
␈↓ ↓H␈↓evaluated␈αwe␈αformed␈αa␈αnew␈αlocal␈αblock,␈αlinking␈αit␈αon␈αto␈αthe␈αfront␈αof␈αthe␈αexisting␈αenvironment,␈αand
␈↓ ↓H␈↓the␈αresulting␈αstructure␈αbecame␈αthe␈αnew␈αenvironment.␈αSince␈αwe␈αneed␈αspace␈αto␈αcontain␈αthe␈αevaluated
␈↓ ↓H␈↓parameters,␈α∂and␈α∂since␈α∞those␈α∂results␈α∂are␈α∂then␈α∞moved␈α∂into␈α∂a␈α∂environment␈α∞block,␈α∂we␈α∂might␈α∂as␈α∞well
␈↓ ↓H␈↓make␈αthe␈αspace␈αwhich␈αis␈αto␈αcontain␈αthose␈αevaluated␈αparameters␈αas␈αmuch␈αlike␈αan␈αenvironment␈αblock
␈↓ ↓H␈↓as␈αpossible.␈α Indeed,␈αthe␈αspace␈αrequirements␈αfor␈αthe␈αevaluated␈αparameters␈αis␈αknown:␈αthe␈αblock␈αmust
␈↓ ↓H␈↓be␈αas␈αlong␈αas␈αthe␈αnumber␈αof␈αformal␈αparameters␈αexpected.␈α This␈αrequirement␈αcan␈αbe␈αascertained␈αby
␈↓ ↓H␈↓examining␈α⊃the␈α⊃definition␈α⊃of␈α∩the␈α⊃function␈α⊃being␈α⊃called.␈α⊃Once␈α∩the␈α⊃block␈α⊃is␈α⊃allocated,␈α∩the␈α⊃actual
␈↓ ↓H␈↓parameters␈αare␈αevaluated␈αand␈αthe␈α
results␈αare␈αsent␈αto␈αthe␈αproper␈α
slot␈αin␈αthe␈αallocated␈αblock.␈α
Such␈αa
␈↓ ↓H␈↓block␈αwill␈αbe␈αcalled␈αa␈α␈↓↓destination␈αblock␈↓;␈αand␈αthe␈αoperation␈αof␈αplacing␈αa␈αresult␈αin␈αa␈αdestination␈αwill
␈↓ ↓H␈↓be␈αcalled␈α␈↓↓sending␈↓.␈α Once␈αall␈αthe␈αevaluated␈αparameters␈αhave␈αbeen␈αsent,␈αwe␈α␈↓↓link␈↓␈αthe␈αcompleted␈αblock
␈↓ ↓H␈↓into␈α_the␈α↔front␈α_of␈α_the␈α↔current␈α_environment.␈α_ The␈α↔ideas␈α_expressed␈α_in␈α↔this␈α_section␈α_are␈α↔an
␈↓ ↓H␈↓embellishment␈α∞of␈α∞those␈α∞of␈α∞page 106.␈α∞ The␈α∞main␈α∞innovation␈α∞is␈α∞to␈α∞allocate␈α∞space␈α∞for␈α∞the␈α
evaluated
␈↓ ↓H␈↓arguments␈α
before␈α
beginning␈αtheir␈α
actual␈α
evaluation.␈αThe␈α
evaluator␈α
sends␈αthe␈α
values␈α
to␈αthe␈α
allocated
␈↓ ↓H␈↓block

␈↓ ↓H␈↓Here are the primitive routines:

␈↓ ↓H␈↓␈↓↓1.␈↓␈α∂␈↓αalloc_dest␈↓:␈α∂This␈α∂unary␈α∂function␈α∂is␈α∂supplied␈α∂with␈α∂the␈α∂formal␈α∂parameter␈α∂list␈α∂of␈α∂a␈α∂function,␈α∞and
␈↓ ↓H␈↓␈↓ αxsupplies␈α→a␈α_new␈α→destination␈α→block␈α_with␈α→the␈α→formal␈α_parameters␈α→placed␈α→in␈α_the
␈↓ ↓H␈↓␈↓ αxname-section␈α∂of␈α∂the␈α∂block.␈α∂An␈α∂internal␈α∂pointer␈α∂is␈α∂initialized␈α∂to␈α∂the␈α∂first␈α∂slot␈α∂in␈α∂the
␈↓ ↓H␈↓␈↓ αxblock. Thus:
␈↓"␈↓ ↓H␈↓
                     ␈↓ a destination block␈↓


␈↓"␈↓ ↓H␈↓
                        ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
                        ~     #αβα⊃
␈↓"␈↓ ↓H␈↓
                        εαααπαααλ ↓ ␈↓internal pointer␈↓

␈↓"␈↓ ↓H␈↓
                        ~   ~   ~←$
␈↓"␈↓ ↓H␈↓
                        εαααβαααλ
␈↓"␈↓ ↓H␈↓
                        ~   ~   ~
␈↓"␈↓ ↓H␈↓
                          # # #
␈↓"␈↓ ↓H␈↓
                        εαααβαααλ
␈↓"␈↓ ↓H␈↓
                        ~   ~   ~
␈↓"␈↓ ↓H␈↓
                        %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
                          ↑   ↑
␈↓"␈↓ ↓H␈↓
              ␈↓formal parameters  values␈↓


␈↓ ↓H␈↓␈↓↓2.␈↓␈α∞␈↓αsend␈↓:␈α
This␈α∞is␈α
a␈α∞binary␈α
function␈α∞whose␈α∞first␈α
argument␈α∞is␈α
a␈α∞destination␈α
block␈α∞and␈α∞whose␈α
second
␈↓ ↓H␈↓␈↓ α8argument␈α
is␈α
a␈αvalue␈α
to␈α
be␈αsent.␈α
The␈α
value␈α
of␈α␈↓αsend␈↓␈α
is␈α
the␈αdestination␈α
block.␈α
The␈α
effect␈αof
␈↓ ↓H␈↓␈↓ α8␈↓αsend␈↓␈αis␈αto␈αsend␈αits␈αsecond␈αargument␈αto␈αthe␈αcurrent␈αdestination␈αslot.␈αThe␈αinternal␈αpointer␈αis
␈↓ ↓H␈↓␈↓ α8␈↓↓not␈↓ updated; that is the business of ␈↓αnext␈↓.
␈↓ ↓H␈↓␈↓↓150  Evaluation␈↓ &4.17␈↓



␈↓ ↓H␈↓␈↓↓3.␈↓␈α␈↓αnext␈↓:␈αThis␈αfunction␈αtakes␈αa␈αdestination␈αblock␈αas␈αargument␈αand␈αmoves␈αthe␈αinternal␈αpointer␈αof␈αthe
␈↓ ↓H␈↓␈↓ α8block␈αto␈αpoint␈αto␈αthe␈αsucceeding␈αslot.␈αThe␈α
value␈αof␈α␈↓αnext␈↓␈αis␈αthe␈αdestination␈αblock.␈αThus␈α
␈↓αnext␈↓
␈↓ ↓H␈↓␈↓ α8is an identity function with a side-effect.

␈↓ ↓H␈↓␈↓↓4.␈↓␈α␈↓αlink␈↓:␈α␈↓αlink␈↓␈αtakes␈αa␈αdestination␈αblock␈α
and␈αan␈αenvironment␈αas␈αarguments␈αand␈αlinks␈α
the␈αdestination
␈↓ ↓H␈↓␈↓ α8block␈α
onto␈α
the␈α
front␈α
of␈α
the␈α
environment.␈αThe␈α
resulting␈α
environment␈α
is␈α
the␈α
value␈α
of␈α␈↓αlink␈↓.
␈↓ ↓H␈↓␈↓ α8Since␈αthe␈αinternal␈αpointer␈αis␈αonly␈αused␈αduring␈αthe␈αfilling␈αof␈αthe␈αdest-block,␈αwe␈αcan␈αassume
␈↓ ↓H␈↓␈↓ α8that ␈↓αlink␈↓ replaces that pointer with a pointer to the previous environment.

␈↓ ↓H␈↓␈↓↓5.␈↓␈α␈↓αreceive␈↓:␈αSometimes␈αwe␈αwill␈αwish␈αto␈αexamine␈αthe␈αresult␈αof␈αa␈αcomputation␈αbefore␈αmaking␈αa␈α
decision
␈↓ ↓H␈↓␈↓ αHon␈α∀how␈α∀to␈α∪proceed.␈α∀In␈α∀particular,␈α∪in␈α∀conditional␈α∀expressions␈α∪we␈α∀must␈α∀evaluate␈α∪the
␈↓ ↓H␈↓␈↓ αHpredicate␈α
position␈αbefore␈α
knowing␈αhow␈α
to␈αhandle␈α
the␈αrest␈α
of␈αthe␈α
conditional.␈α
The␈αunary
␈↓ ↓H␈↓␈↓ αHprimitive␈α␈↓αreceive␈↓␈αlets␈αus␈αlook␈αat␈αthe␈αresult␈αof␈αa␈αcomputation.␈αThe␈αargument␈αto␈α␈↓αreceive␈↓␈αis␈αa
␈↓ ↓H␈↓␈↓ αHdestination block, and the value returned is the value in the current slot.

␈↓ ↓H␈↓In␈α_the␈α_following␈α→evaluators␈α_we␈α_will␈α_freely␈α→use␈α_the␈α_extended␈α_conditional␈α→expressions␈α_and
␈↓ ↓H␈↓λ-expressions␈αintroduced␈αon␈αpage 143␈αand␈αpage 146.␈α
 The␈αfirst␈αevaluator␈αis␈αnamed␈α␈↓αdeval␈↓,␈α
with␈αthe
␈↓ ↓H␈↓"␈↓αd␈↓ coming from "destination".

␈↓ ↓H␈↓αdeval <= λ[[exp;env;dest]
␈↓ ↓H␈↓α␈↓ β([isconst[exp] → send[dest;denote[exp]];
␈↓ ↓H␈↓α␈↓ β( isvar[exp] → send[dest;lookup[exp;env]];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → deval␈↓β1␈↓α[func[exp]; arglist[exp]; env; dest] ]]

␈↓ ↓H␈↓αdeval␈↓β1␈↓α <= λ[[fun;args;env;dest]
␈↓ ↓H␈↓α␈↓ β([atom[fun] →␈↓ ∧h[iscond[fun] → devcond[args;env;dest]
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h isprim[fun] → execute[␈↓ π_fun;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h␈↓ ελ␈↓ π_link[␈↓ πhevalargs[␈↓ λXargs;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h␈↓ ελ␈↓ π_␈↓ πh␈↓ λXenv;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h␈↓ ελ␈↓ π_␈↓ πh␈↓ λXalloc_dest[createvars[args]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h␈↓ ελ␈↓ π_␈↓ πhenv];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h␈↓ ελ␈↓ π_dest];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h ␈↓
t␈↓α → deval[fun;env;dest]; deval␈↓β1␈↓α[receive[dest];args;env;dest]  ]
␈↓ ↓H␈↓α␈↓ β( islambda[fun] → evalargs[␈↓ ελbodylist[fun];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h␈↓ ελlink[evalargs[args;env;alloc_dest[vars[fun]]]];env];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧h␈↓ ελdest]
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → deval[fun;env;dest]; deval␈↓β1␈↓α[receive[dest];args;env;dest] ]]
␈↓ ↓H␈↓␈↓↓4.17␈↓ λH␈↓αeval␈↓ with explicit access     151␈↓α



␈↓ ↓H␈↓αevalargs <= λ[[args;env;dest]
␈↓ ↓H␈↓α␈↓ β([null[args] →dest;
␈↓ ↓H␈↓α␈↓ β( null[rest[args]] → deval[first[args];env;dest];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → deval[␈↓ ∧8first[args];env;dest];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧8next[dest];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧8evalargs[rest[args];env;dest] ]]

␈↓ ↓H␈↓αexecute <= λ[[fun;env;dest]send[dest;apply[fun;vals[env];( )]]]


␈↓ ↓H␈↓Note that ␈↓αexecute␈↓ resorts to ␈↓αapply␈↓ to handle primitive application.


␈↓ ↓H␈↓αdevcond <= λ[[args;env;dest]
␈↓ ↓H␈↓α␈↓ β(deval[pred[first[args]];env;dest];
␈↓ ↓H␈↓α␈↓ β([receive[dest] → evalargs[condbody[first[args]];env;dest];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → devcond[rest[args]; env;dest] ]]

␈↓ ↓H␈↓This␈αnew␈αevaluator␈αmust␈αbe␈αsupplied␈αwith␈αan␈αinitial␈αdestination␈αas␈αwell␈αas␈αbeing␈αsupplied␈αwith␈αan
␈↓ ↓H␈↓initial␈αsymbol␈α
table.␈αAlso␈α
since␈αthe␈α
result␈αof␈α
any␈αcalculation␈α
is␈αa␈α
destination␈αblock␈α
rather␈αthan␈αjust␈α
a
␈↓ ↓H␈↓simple␈αvalue,␈αwe␈αshould␈αsupply␈αa␈αselector␈αto␈αextract␈αthe␈αdesired␈αvalue.␈αFor␈αexample,␈αif␈αwe␈αdesignate
␈↓ ↓H␈↓␈↓αval␈↓ as such a selector, and designate the atom ␈↓αTLB␈↓ as the repository for the top level binding then:

␈↓ ↓H␈↓α␈↓ β]eval <= λ[[exp;env] val[deval[exp;env;alloc_dest[(TLB)]]]]

␈↓ ↓H␈↓More␈α∪of␈α∪the␈α∀details␈α∪of␈α∪argument␈α∀handling␈α∪should␈α∪now␈α∀be␈α∪understandable:␈α∪when␈α∀a␈α∪function
␈↓ ↓H␈↓application␈αhas␈αbeen␈αrecognized,␈αthe␈αevaluator␈αsets␈αup␈αa␈αblock␈αto␈αhold␈αthe␈αresults␈αof␈αevaluating␈αthe
␈↓ ↓H␈↓actual␈αparameters.␈αIf␈α
the␈αfunction␈αis␈α
a␈αprimitive␈αfunction␈α
then␈αthe␈αname␈α
slots␈αare␈αfilled␈α
with␈αsome
␈↓ ↓H␈↓system-created names, otherwise the λ-variables are used.



␈↓ ↓H␈↓␈↓ ε↔␈↓↓Problem␈↓

␈↓ ↓H␈↓␈↓ αHUsing the new evaluator, sketch the evaluation of ␈↓αf[A]␈↓ where: ␈↓αf <= λ[[x]eq[x;A]]␈↓.

␈↓ ↓H␈↓Notice␈α
that␈α
for␈α
most␈α
of␈αthe␈α
evaluation␈α
process,␈α
␈↓αdest␈↓␈α
is␈α
a␈αpassive␈α
element.␈α
 A␈α
new␈α
destination␈αblock␈α
is
␈↓ ↓H␈↓created␈α
on␈α
function␈α
applications,␈α
but␈α
that␈α
␈↓αdest␈↓␈α
is␈α
passed␈α
around␈α
as␈α
an␈α
argument␈α
through␈α∞most␈α
of
␈↓ ↓H␈↓the␈α
pieces␈α∞of␈α
the␈α∞evaluator␈α
without␈α∞explicit␈α
modification.␈α
That␈α∞is,␈α
in␈α∞most␈α
λ-bindings␈α∞␈↓αdest␈↓␈α
simply
␈↓ ↓H␈↓gets␈αbound␈αto␈α␈↓αdest␈↓.␈α Since␈αthe␈αλ-binding␈αprocess␈αis␈αnot␈αinexpensive␈αit␈αis␈αtempting␈αto␈αmake␈αvariables
␈↓ ↓H␈↓like␈α⊃␈↓αdest␈↓,␈α⊃which␈α⊃change␈α⊃infrequently,␈α⊃into␈α⊃non-local␈α⊃variables;␈α⊃they␈α⊃would␈α⊃be␈α⊃initialized␈α⊃at␈α⊂the
␈↓ ↓H␈↓outside␈α∞layer␈α∂and␈α∞modified␈α∞by␈α∂side-effects␈α∞during␈α∂the␈α∞evaluation.␈α∞ However␈α∂the␈α∞current␈α∂value␈α∞of
␈↓ ↓H␈↓␈↓↓152  Evaluation␈↓ &4.17␈↓


␈↓ ↓H␈↓␈↓αdest␈↓␈α
␈↓↓does␈↓␈α
need␈αto␈α
be␈α
saved␈αoccasionally.␈α
Those␈α
occasions␈αcorrespond␈α
to␈α
the␈αplaces␈α
in␈α
␈↓αdeval␈↓␈α␈↓π 80␈↓␈α
where
␈↓ ↓H␈↓␈↓αdest␈↓␈α∂gets␈α⊂rebound␈α∂to␈α⊂something␈α∂other␈α∂than␈α⊂␈↓αdest␈↓.␈α∂ We␈α⊂will␈α∂supply␈α∂two␈α⊂new␈α∂primitives␈α⊂to␈α∂handle
␈↓ ↓H␈↓explicit saving and restoring of values:

␈↓ ↓H␈↓␈↓↓1.␈↓␈α␈↓αsave␈↓:␈α
This␈αbinary␈αfunction␈α
would␈αbe␈αimplemented␈α
as␈αa␈αspecial␈α
form.␈α Its␈αfirst␈α
argument␈αis␈αa␈α
name
␈↓ ↓H␈↓␈↓ α(␈↓αold␈↓,␈α
and␈αits␈α
second␈α
argument␈αis␈α
a␈αvalue␈α
␈↓αnew␈↓.␈α
 The␈αcurrent␈α
value␈α
associated␈αwith␈α
␈↓αold␈↓␈αis␈α
saved,
␈↓ ↓H␈↓␈↓ α(and the value ␈↓αnew␈↓ becomes the value of ␈↓αold␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α␈↓αrestore␈↓:␈α
This␈αis␈α
a␈αunary␈α
function;␈αits␈α
argument␈αis␈α
a␈αname␈α
␈↓αname␈↓.␈αThe␈α
latest␈αvalue␈α
which␈αwas␈α
saved
␈↓ ↓H␈↓␈↓ αHfor ␈↓αname␈↓ is restored. The value which ␈↓αname␈↓ had on entry to ␈↓αrestore␈↓ is lost.

␈↓ ↓H␈↓Using ␈↓αsave␈↓ and ␈↓αrestore␈↓ we could express the evaluation of a λ-application something like:
␈↓ ↓H␈↓α␈↓ αXeval[␈↓
R␈↓∞(␈↓α λ[[x;y]␈↓λx␈↓α][a;b] ␈↓∞)␈↓α; env] =␈↓ ελsave[x;a];
␈↓ ↓H␈↓α␈↓ αX␈↓ ελsave[y;b];
␈↓ ↓H␈↓α␈↓ αX␈↓ ελeval[␈↓
R␈↓∞( ␈↓λx ␈↓∞)␈↓α ;env␈↓λ'␈↓α];
␈↓ ↓H␈↓α␈↓ αX␈↓ ελrestore[y];
␈↓ ↓H␈↓α␈↓ αX␈↓ ελrestore[x];

␈↓ ↓H␈↓The␈α⊂implementation␈α⊂details␈α⊂of␈α⊂␈↓αsave␈↓␈α⊂and␈α⊂␈↓αrestore␈↓␈α⊃will␈α⊂not␈α⊂be␈α⊂needed␈α⊂for␈α⊂most␈α⊂of␈α⊃our␈α⊂discussion,
␈↓ ↓H␈↓however␈α⊂we␈α⊂include␈α∂some␈α⊂of␈α⊂them␈α⊂here␈α∂for␈α⊂completeness.␈α⊂The␈α∂information␈α⊂which␈α⊂is␈α⊂␈↓αsave␈↓d␈α∂and
␈↓ ↓H␈↓␈↓αrestore␈↓d␈α⊂is␈α⊂accessible␈α∂through␈α⊂a␈α⊂global␈α∂variable␈α⊂named␈α⊂␈↓αcontrol␈↓.␈α∂A␈α⊂␈↓αsave[␈↓<name>;<value>]␈α⊂has␈α∂the
␈↓ ↓H␈↓effect␈αof␈α
␈↓αconcat␈↓-ing␈αthe␈αcurrent␈α
value␈αof␈α
<name>␈αonto␈αthe␈α
front␈αof␈α
␈↓αcontrol␈↓;␈αit␈αthen␈α
sets␈αthe␈αnew␈α
value
␈↓ ↓H␈↓of <name> to <value>.
␈↓ ↓H␈↓That is: ␈↓ β␈↓αcontrol ← concat[eval[␈↓<name>␈↓α;env];control];set[␈↓<name>; ␈↓αeval[␈↓<value>␈↓α;env]␈↓

␈↓ ↓H␈↓A ␈↓αrestore␈↓[<name>] performs:
␈↓ ↓H␈↓α␈↓ ∧≡set␈↓[<name>␈↓α;first[control]]; control ← rest[control]

␈↓ ↓H␈↓The␈α∞manipulation␈α
of␈α∞␈↓αcontrol␈↓␈α∞by␈α
␈↓αsave␈↓␈α∞and␈α∞␈↓αrestore␈↓␈α
is␈α∞stack-like␈α
in␈α∞LISP.␈α∞That␈α
means␈α∞that␈α∞only␈α
the
␈↓ ↓H␈↓␈↓αfirst␈↓␈αelement␈αof␈α␈↓αcontrol␈↓␈αis␈αaccessible;␈αto␈αaccess␈αelements␈αin␈αthe␈αinterior␈αof␈α␈↓αcontrol␈↓␈αrequires␈α␈↓αrestore␈↓-ing
␈↓ ↓H␈↓down␈α
to␈α
them␈α
by␈α
sequence␈α
of␈α
"␈↓αcontrol␈↓ ← ␈↓αrest[control]␈↓".␈α
 Once␈α
we␈α
have␈α
removed␈α
elements␈αfrom␈α
␈↓αcontrol␈↓
␈↓ ↓H␈↓there␈α
is␈α
no␈αway␈α
to␈α
access␈αthat␈α
information␈α
again.␈α The␈α
␈↓αcontrol␈↓-structure␈α
is␈αnot␈α
accessible␈α
as␈α
a␈αdata
␈↓ ↓H␈↓structure␈α∪to␈α∪the␈α∀same␈α∪degree␈α∪of␈α∪generality␈α∀as␈α∪is␈α∪the␈α∪access␈α∀structure.␈α∪The␈α∪closest␈α∀analogy␈α∪to
␈↓ ↓H␈↓␈↓αfunction-funarg␈↓␈α⊃is␈α⊃the␈α∩␈↓αcatch-throw␈↓␈α⊃pair.␈α⊃However␈α∩now␈α⊃that␈α⊃␈↓αcontrol␈↓␈α⊃␈↓↓is␈↓␈α∩explicit␈α⊃we␈α⊃can␈α∩begin␈α⊃to
␈↓ ↓H␈↓describe extensions to LISP which ␈↓↓will␈↓ allow us to capture ␈↓αcontrol␈↓ like ␈↓αfunction␈↓ captures ␈↓αenv␈↓.

␈↓ ↓H␈↓Given␈α␈↓αsave␈↓␈αand␈α␈↓αrestore␈↓␈α
we␈αcan␈αrewrite␈α␈↓αdeval␈↓␈αand␈α
its␈αsubfunctions␈αto␈αaccess␈αnon-local␈α
representations
␈↓ ↓H␈↓of␈αvariables␈αused␈αin␈αthe␈αcurrent␈α␈↓αdeval␈↓.␈α Thus␈αthe␈αevaluator␈αbecomes␈αa␈αfunction␈αof␈αno␈αarguments;␈αit
␈↓ ↓H␈↓␈↓↓knows␈↓␈αwhere␈αto␈αfind␈αthe␈αvalues␈αand␈αit␈αknows␈αhow␈αto␈αsave␈αand␈αrestore␈αthose␈αvariables.␈α The␈αresult
␈↓ ↓H␈↓is an evaluator which has even ␈↓↓fewer␈↓ implict operations than ␈↓αdeval␈↓.




␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 80␈↓ and its sub-functions
␈↓ ↓H␈↓␈↓↓4.17␈↓ λH␈↓αeval␈↓ with explicit access     153␈↓α



␈↓ ↓H␈↓αdeval␈↓λ'␈↓α <= λ[[]␈↓ β([isconst[] → send[denote[]];
␈↓ ↓H␈↓α␈↓ β( isvar[] → send[lookup[]];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →␈↓ βhsave[fun;func[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhsave[args;arglist[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhdeval␈↓β1␈↓α[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhrestore[args];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhrestore[fun] ]]

␈↓ ↓H␈↓Before␈α
continuing,␈α
a␈α
few␈α
points␈α∞should␈α
be␈α
made.␈α
We␈α
will␈α
be␈α∞using␈α
the␈α
same␈α
names␈α
as␈α
we␈α∞did␈α
in
␈↓ ↓H␈↓␈↓αdeval␈↓␈α∂for␈α∂all␈α∂subfunctions␈α∂of␈α∞␈↓αdeval␈↓λ'␈↓.␈α∂The␈α∂difference␈α∂will␈α∂be␈α∞that␈α∂here␈α∂those␈α∂functions␈α∂will␈α∞␈↓↓know␈↓
␈↓ ↓H␈↓where␈αtheir␈α
arguments␈αare␈α
to␈αbe␈αfound;␈α
they␈αneed␈α
not␈αbe␈αexplicitly␈α
passed␈αin.␈α
Thus␈α␈↓αsend[denote[]]␈↓
␈↓ ↓H␈↓means␈αthat␈α
␈↓αdenote␈↓␈αextracts␈αa␈α
value␈αfrom␈α
the␈αrepresentation␈αin␈α
␈↓αexp␈↓␈αand␈α
␈↓αsend␈↓␈αknows␈αthat␈α
it␈αis␈αto␈α
send
␈↓ ↓H␈↓its value to ␈↓αdest␈↓.

␈↓ ↓H␈↓With this new evaluator we can define ␈↓αeval␈↓ as:

␈↓ ↓H␈↓α␈↓ β(eval <= λ[[x;y]␈↓ ∧xfun ← ();
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧xargs ← ();
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧xexp ← x;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧xenv ← y;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧xdest ← alloc_dest[(TLB)];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧xdeval␈↓λ'␈↓α[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧xval[dest]]

␈↓ ↓H␈↓α␈↓Here is the remainder of ␈↓αdeval␈↓λ'␈↓:

␈↓ ↓H␈↓αdeval␈↓β1␈↓α <= λ[[]␈↓ β([isatom[] →␈↓ ∧X[iscond[] → devcond[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X isprim[] →␈↓ ¬hsave[env;env];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hsave[dest;alloc_dest[createvars[]]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hevalargs[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hlink[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hrestore[dest];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hexecute[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hrestore[env];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X ␈↓
t␈↓α → deval␈↓β2␈↓α[] ]
␈↓ ↓H␈↓α␈↓ β(islambda[] →␈↓ ∧Xsave[env;env];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xsave[dest;alloc_dest[vars[]]
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xevalargs[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xlink[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xrestore[dest];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xsave[args;bodylist[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xevalargs[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xrestore[args];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xrestore[env];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → deval␈↓β2␈↓α[] ]]
␈↓ ↓H␈↓␈↓↓154  Evaluation␈↓ &4.17␈↓



␈↓ ↓H␈↓αdeval␈↓β2␈↓α <= λ[[ ]␈↓ β_save[exp;fun];
␈↓ ↓H␈↓α␈↓ β_deval␈↓λ'␈↓α[];
␈↓ ↓H␈↓α␈↓ β_restore[exp];
␈↓ ↓H␈↓α␈↓ β_save[fun;receive[]];
␈↓ ↓H␈↓α␈↓ β_deval␈↓β1␈↓α[];
␈↓ ↓H␈↓α␈↓ β_restore[fun] ]

␈↓ ↓H␈↓We␈α
introduced␈α
␈↓αdeval␈↓β2␈↓␈α
to␈α
capture␈α
the␈α
computation␈α
to␈α
be␈α
performed␈α
when␈α
the␈α
function-position␈αis␈α
not
␈↓ ↓H␈↓recognized␈α⊗as␈α⊗either␈α⊗a␈α⊗λ-expression,␈α⊗a␈α↔conditional,␈α⊗or␈α⊗a␈α⊗primitive.␈α⊗ Note␈α⊗that␈α↔we␈α⊗perform
␈↓ ↓H␈↓␈↓αsave[env;env]␈↓␈α∞in␈α
a␈α∞couple␈α∞of␈α
places␈α∞in␈α∞␈↓αdeval␈↓β1␈↓.␈α
 This␈α∞is␈α∞necessary␈α
to␈α∞save␈α∞the␈α
current␈α∞value␈α∞of␈α
␈↓αenv␈↓
␈↓ ↓H␈↓since␈α␈↓αlink␈↓␈αmodifies␈α␈↓αenv␈↓.␈α
Indeed,␈αthe␈αsequence:␈αsave␈α
␈↓αenv␈↓␈αand␈α␈↓αdest␈↓,␈α␈↓αevalargs␈↓,␈α
␈↓αlink␈↓,␈αand␈αrestore␈α␈↓αdest␈↓␈α
can
␈↓ ↓H␈↓be simplified to: save ␈↓αdest␈↓, ␈↓αevalargs␈↓, followed by ␈↓αlink␈↓λ'␈↓.  where:
␈↓ ↓H␈↓α␈↓ βYlink␈↓λ'␈↓α <=λ[[] set_int[dest;env];rotate[env;first[control];dest]]

␈↓ ↓H␈↓and␈α␈↓αset_int␈↓␈αsets␈αthe␈αinternal␈αpointer␈αof␈αthe␈αdest-block␈αto␈αthe␈αcurrent␈αenvironment,␈αand␈α␈↓αrotate[x;y;z]␈↓
␈↓ ↓H␈↓moves the contents of ␈↓αx␈↓ to ␈↓αy␈↓, contents of ␈↓αy␈↓ to ␈↓αz␈↓, and contents of ␈↓αz␈↓ to ␈↓αx␈↓.

␈↓ ↓H␈↓αevalargs <= λ[[]␈↓ β([emptyargs[] → ( );
␈↓ ↓H␈↓α␈↓ β( singlearg[] →␈↓ ∧Xsave[exp;first[args]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βx␈↓ ∧Xdeval␈↓λ'␈↓α[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βx␈↓ ∧Xrestore[exp];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →␈↓ βxsave[exp;first[args]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxdeval␈↓λ'␈↓α[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxrestore[exp];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxnext[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxsave[args;rest[args]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxevalargs[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxrestore[args] ]]

␈↓ ↓H␈↓Note␈α⊂that␈α⊂we␈α⊂are␈α⊂never␈α⊃interested␈α⊂in␈α⊂the␈α⊂value␈α⊂returned␈α⊂from␈α⊃a␈α⊂call␈α⊂on␈α⊂a␈α⊂sub-function␈α⊃in␈α⊂the
␈↓ ↓H␈↓evaluator;␈α∂all␈α∞values␈α∂are␈α∞passed␈α∂explicity␈α∞from␈α∂their␈α∞creator␈α∂to␈α∞a␈α∂destination.␈α∞We␈α∂might␈α∂say␈α∞that
␈↓ ↓H␈↓␈↓αdeval␈↓λ'␈↓␈αnever␈αreturns␈αa␈αvalue.␈αIn␈αthe␈αnext␈αsection␈αwe␈αwill␈αbuild␈αan␈αevaluator␈αwhich␈αnever␈αreturns␈αat
␈↓ ↓H␈↓all.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Write the new version of ␈↓αdevcond␈↓.

␈↓ ↓H␈↓II.␈α∂Examine␈α∂the␈α∂␈↓αsave-restore␈↓␈α∂sequences␈α∂in␈α∂␈↓αdeval␈↓λ'␈↓␈α∂and␈α∂its␈α∂sub-functions␈α∂for␈α⊂possible␈α∂inefficiencies.
␈↓ ↓H␈↓That␈α⊃is,␈α⊃are␈α⊃all␈α⊃the␈α⊂saves␈α⊃and␈α⊃restores␈α⊃necessary␈α⊃or␈α⊂could␈α⊃explicit␈α⊃assignments␈α⊃to␈α⊃some␈α⊃of␈α⊂the
␈↓ ↓H␈↓non-local variables speed things up?
␈↓ ↓H␈↓␈↓↓4.17␈↓ λH␈↓αeval␈↓ with explicit access     155␈↓α


␈↓ ↓H␈↓III. Using the new evaluator, sketch the evaluation of ␈↓αf[A]␈↓ where: ␈↓αf <= λ[[x]eq[x;A]]␈↓.



␈↓ ↓H␈↓␈↓ ¬
␈↓↓4.18  ␈↓αeval␈↓ with explicit control␈↓α


␈↓ ↓H␈↓Recursion␈α
and␈α
call-by-value␈α∞are␈α
used␈α
to␈α
guide␈α∞the␈α
flow␈α
of␈α
control␈α∞in␈α
a␈α
LISP␈α
evaluator.␈α∞We␈α
have
␈↓ ↓H␈↓started␈α_to␈α_explore␈α_the␈α_implementation␈α_of␈α_call-by-value,␈α_and␈α_now␈α_we␈α_wish␈α_to␈α_discuss␈α↔the
␈↓ ↓H␈↓implementation␈αof␈αrecursion.␈αThe␈αmechanisms␈αused␈α
in␈αthe␈αimplementation␈αof␈αany␈αconcept␈α
must␈αbe
␈↓ ↓H␈↓of␈α∞a␈α∞higher␈α∞level␈α∞of␈α∞detail␈α∞than␈α∞the␈α∞mechanism␈α∞being␈α∞implemented.␈α∞ We␈α∞cannot␈α∞use␈α∞recursion␈α
to
␈↓ ↓H␈↓implement␈α∞recursion.␈α
The␈α∞basic␈α
purpose␈α∞of␈α∞recursive␈α
control␈α∞in␈α
the␈α∞evaluator␈α
is␈α∞to␈α∞describe␈α
what
␈↓ ↓H␈↓computation␈α∞to␈α∂perform␈α∞next␈α∂and␈α∞to␈α∂describe␈α∞where␈α∂to␈α∞go␈α∂when␈α∞finished.␈α∂The␈α∞evaluator␈α∂of␈α∞this
␈↓ ↓H␈↓section␈αwill␈α
rely␈αon␈α
explicit␈αdirections␈αto␈α
tell␈αit␈α
what␈αto␈α
do␈αnext.␈α The␈α
idea␈αis␈α
closely␈αrelated␈α
to␈αthe
␈↓ ↓H␈↓logical␈α
notion␈αof␈α
␈↓↓continuations␈↓␈α
([Wad 74]␈αand␈α
[Fis 72])␈α
and␈αthus␈α
we␈α
will␈αuse␈α
that␈αterminology␈α
here.
␈↓ ↓H␈↓In␈α
the␈α
evaluators␈αof␈α
this␈α
section␈αwe␈α
will␈α
use␈αthe␈α
destination␈α
to␈αtell␈α
where␈α
the␈αresult␈α
of␈α
the␈αcurrent
␈↓ ↓H␈↓computation is to be put, and use the continuation to tell what the next computation will be.

␈↓ ↓H␈↓Note that the computations in ␈↓αdeval␈↓ are basically of two categories:

␈↓ ↓H␈↓␈↓ α_␈↓↓1.␈↓␈α∩Simple␈α∪transformations␈α∩like␈α∩sending,␈α∪building␈α∩␈↓αdest␈↓␈α∩blocks,␈α∪or␈α∩selecting␈α∪components␈α∩of
␈↓ ↓H␈↓␈↓ α_expressions.␈α→These␈α→computations␈α→are␈α→non-recursive,␈α→requiring␈α→a␈α→bounded␈α→amount␈α_of
␈↓ ↓H␈↓␈↓ α_computation.

␈↓ ↓H␈↓␈↓ α_␈↓↓2.␈↓␈αRecursive␈αcalls␈αon␈αthe␈αevaluator␈αor␈αits␈αsubfunctions.␈αThese␈αcomputations␈αcan␈αbe␈αarbitrarily
␈↓ ↓H␈↓␈↓ α_complex.

␈↓ ↓H␈↓It␈αis␈αthe␈α
recursive␈αcomputations␈αwhich␈αwe␈α
wish␈αto␈αexamine.␈αOne␈α
of␈αthe␈αimplications␈αof␈α
a␈αfunction
␈↓ ↓H␈↓call␈α⊂is␈α∂that␈α⊂we␈α∂have␈α⊂further␈α∂computation␈α⊂to␈α∂be␈α⊂performed␈α∂␈↓↓after␈↓␈α⊂the␈α∂call␈α⊂is␈α∂completed.␈α⊂It␈α⊂is␈α∂the
␈↓ ↓H␈↓responsibility␈α∞of␈α
the␈α∞evaluator␈α∞to␈α
remember␈α∞where␈α∞a␈α
computation␈α∞has␈α∞been␈α
interrupted␈α∞so␈α∞that␈α
it
␈↓ ↓H␈↓may␈α∃pick␈α∃up␈α∃where␈α∃it␈α∃left␈α∃off,␈α∃after␈α∃completing␈α∃the␈α∃call.␈α∃ One␈α∃of␈α∃the␈α∃major␈α∃problems␈α∃in
␈↓ ↓H␈↓implementing␈αevaluators␈αis␈α"how␈αto␈αremember".␈α If␈αthe␈αfunction␈αbeing␈αcalled␈αis␈αa␈αsimple␈αcalculation
␈↓ ↓H␈↓of␈αtype␈α␈↓↓1.␈↓␈αabove,␈αthen␈αwe␈αcould␈αreplace␈αthe␈αcall␈αwith␈αa␈αcopy␈αof␈αthe␈αbody␈αof␈αthe␈αdefinition␈αwhere␈αwe
␈↓ ↓H␈↓have␈α
replaced␈α
each␈α
occurrence␈α
of␈α
a␈α
formal␈α
parameter␈α
with␈α
the␈α
appropriate␈α
actual␈α∞parameter;␈α
this
␈↓ ↓H␈↓works␈α
nicely.␈α
 Indeed␈α
making␈αsuch␈α
formal␈α
substitutions␈α
works␈α
for␈αcomputations␈α
of␈α
type␈α
␈↓↓2.␈↓␈α
as␈αwell.
␈↓ ↓H␈↓However the solution in this case is not sufficiently efficient.

␈↓ ↓H␈↓In␈αprevious␈αevaluators␈αmuch␈αof␈αthe␈α"remembering"␈αwas␈αdone␈αby␈αrecursion␈αin␈α␈↓αeval␈↓␈α(Section 4.5)␈αand
␈↓ ↓H␈↓by␈αexplicit␈αsequencing␈αin␈α␈↓αdeval␈↓.␈α We␈αnow␈αpropose␈αto␈αexplicitly␈α␈↓↓pass␈αalong␈↓␈αinformation␈αabout␈αwhat
␈↓ ↓H␈↓to do after the current computation is completed. This information is called the ␈↓↓continuation␈↓.

␈↓ ↓H␈↓The␈α
first␈αevaluator␈α
of␈αthis␈α
section␈αis␈α
␈↓αceval␈↓␈↓π 81␈↓;␈αit␈α
is␈αa␈α
modification␈αof␈α
␈↓αdeval␈↓λ'␈↓␈αof␈α
page 153.␈αIt␈α
takes␈αa
␈↓ ↓H␈↓single␈αargument␈α␈↓αc␈↓␈α
which␈αis␈αa␈αcontinutation.␈α
 The␈αcontinutation␈αis␈αpassed␈α
along␈αas␈αa␈α
data␈αstructure

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 81␈↓ ␈↓αc␈↓ for ␈↓↓c␈↓ontrol or ␈↓↓c␈↓ontinuation
␈↓ ↓H␈↓␈↓↓156  Evaluation␈↓ %4.18␈↓


␈↓ ↓H␈↓until␈α⊂␈↓αceval␈↓␈α∂has␈α⊂completed␈α⊂its␈α∂current␈α⊂computation.␈α∂At␈α⊂that␈α⊂time␈α∂␈↓αc␈↓␈α⊂is␈α∂executed.␈α⊂ For␈α⊂example␈α∂we
␈↓ ↓H␈↓transform␈α
␈↓αdeval␈↓λ'␈↓␈αinto␈α
␈↓αceval␈↓␈α
by␈αforming␈α
a␈α
continuation␈αfrom␈α
that␈α
portion␈αof␈α
␈↓αdeval␈↓λ'␈↓␈α
which␈αfollows␈α
the
␈↓ ↓H␈↓call on ␈↓αdeval␈↓β1␈↓. Thus:

␈↓ ↓H␈↓αceval <= λ[[c]
␈↓ ↓H␈↓α␈↓ αH[isconst[] → send[denote[]];c[];
␈↓ ↓H␈↓α␈↓ αH isvar[] → send[lookup[]];c[];
␈↓ ↓H␈↓α␈↓ αH ␈↓
t␈↓α →␈↓ βλsave[fun;func[]];
␈↓ ↓H␈↓α␈↓ αH␈↓ βλsave[args;arglist[]];
␈↓ ↓H␈↓α␈↓ αH␈↓ βλceval␈↓β1␈↓α[function[ev1]] ]]

␈↓ ↓H␈↓αev1 <= λ[[ ] restore[args]; restore[func]; c[] ]]

␈↓ ↓H␈↓For␈α∂the␈α∂simple␈α∂cases␈α∂we␈α∂just␈α∂execute␈α∂the␈α∂continuation␈α∂after␈α∂the␈α∂␈↓αsend␈↓;␈α∂when␈α∂we␈α∂have␈α∂a␈α∞function
␈↓ ↓H␈↓application␈αwe␈α
make␈αup␈αa␈α
new␈αcontinuation.␈α When␈α
␈↓αceval␈↓β1␈↓␈αis␈αfinished␈α
with␈αthe␈αfunction␈α
application
␈↓ ↓H␈↓it executes ␈↓αev1␈↓; that does the ␈↓αrestore␈↓ operations and then performs the saved continuation.

␈↓ ↓H␈↓Note␈α∞the␈α
use␈α∞of␈α
␈↓αfunction␈↓.␈α∞The␈α
non-local␈α∞variable␈α
␈↓αc␈↓␈α∞in␈α
␈↓αev1␈↓␈α∞represents␈α
the␈α∞continuation␈α∞passed␈α
into
␈↓ ↓H␈↓␈↓αceval␈↓.␈αTherefore␈α␈↓αc␈↓␈αmust␈αbe␈αfound␈αin␈αthe␈αenvironment␈αof␈αthe␈αbody␈αof␈α␈↓αceval␈↓␈α␈↓↓not␈↓␈αin␈αthe␈αenvironment
␈↓ ↓H␈↓which is current when ␈↓αev1␈↓ is applied.

␈↓ ↓H␈↓As before, ␈↓αeval␈↓ is expressible with the evaluator:

␈↓ ↓H␈↓α␈↓ β(eval <= λ[[x;y]␈↓ ¬λfun ← ();
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬λargs ← ();
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬λexp ← x;
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬λenv ← y;
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬λdest ← alloc_dest[(TLB)];
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬λceval[function[λ[[ ]val[dest]]]] ]

␈↓ ↓H␈↓Transforming␈α∞the␈α∂sub-functions␈α∞of␈α∞␈↓αdeval␈↓λ'␈↓␈α∂is␈α∞reasonably␈α∞straightforward:␈α∂the␈α∞segment␈α∂of␈α∞program
␈↓ ↓H␈↓below␈α
a␈αcall␈α
on␈αone␈α
of␈αthe␈α
recursive␈α
parts␈αof␈α
the␈αevaluator␈α
is␈αnamed;␈α
a␈αnew␈α
continuation␈α
is␈αmade
␈↓ ↓H␈↓like␈αwe␈α
did␈αfor␈α␈↓αev1␈↓;␈α
the␈αtransformation␈αprocess␈α
is␈αapplied␈αto␈α
each␈αnew␈αcontinuation.␈α
 For␈αexample,
␈↓ ↓H␈↓here's ␈↓αceval␈↓β1␈↓:

␈↓ ↓H␈↓αceval␈↓β1␈↓α <= λ[[c]␈↓ β([isatom[] →␈↓ ∧X[iscond[] → devcond[c];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X isprim[] →␈↓ ¬hsave[env;env];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hsave[dest;alloc_dest[createvars[]]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬hevalargs[function[ev2]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X ␈↓
t␈↓α → ceval␈↓β2␈↓α[];
␈↓ ↓H␈↓α␈↓ β(islambda[] →␈↓ ∧Xsave[env;env]
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xsave[dest;alloc_dest[vars[]]
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧Xevalargs[function[ev5]];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → ceval␈↓β2␈↓α[]] ]]

␈↓ ↓H␈↓␈↓αceval␈↓β2␈↓α <= λ[[ ] save[exp;fun]; ceval[function[ev3]]]␈↓
␈↓ ↓H␈↓␈↓↓4.18␈↓ λ:␈↓αeval␈↓ with explicit control     157␈↓α



␈↓ ↓H␈↓αev2 <= λ[[ ] link[]; restore[dest]; execute[]; restore[env]; c[]]

␈↓ ↓H␈↓αev3 <= λ[[ ] restore[exp]; save[fun;receive[]]; ceval␈↓β1␈↓α[function[ev4]]]

␈↓ ↓H␈↓αev4 <= λ[[ ]restore[fun]; c[]]

␈↓ ↓H␈↓αev5 <= λ[[ ] link[]; restore[dest]; save[args;bodylist[]]; evalargs[function[ev6]]]

␈↓ ↓H␈↓αev6 <= λ[[ ] restore[args]; restore[env]; c[]]



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓␈↓ ∧⊂I. Write the new version of ␈↓αdevcond␈↓ and ␈↓αevalargs␈↓.

␈↓ ↓H␈↓␈↓ α5II. Using the new evaluator, sketch the evaluation of ␈↓αf[A]␈↓ where: ␈↓αf <= λ[[x]eq[x;A]]␈↓.

␈↓ ↓H␈↓The␈α
final␈α
step␈αis␈α
analogous␈α
to␈αthat␈α
which␈α
we␈αperformed␈α
moving␈α
from␈α␈↓αdeval␈↓␈α
to␈α
␈↓αdeval␈↓λ'␈↓:␈α
we␈αremove
␈↓ ↓H␈↓the␈α
argument␈α
to␈α
␈↓αceval␈↓␈αand␈α
pass␈α
the␈α
continuation␈α
explicitly␈αin␈α
a␈α
non-local␈α
variable␈α
named␈α␈↓αcont␈↓.␈α
 This
␈↓ ↓H␈↓new evaluator is named ␈↓αceval␈↓λ'␈↓:

␈↓ ↓H␈↓αceval␈↓λ'␈↓α <= λ[[ ]␈↓ β([isconst[] → send[denote[]]; cont[];
␈↓ ↓H␈↓α␈↓ β( isvar[] → send[lookup[]]; cont[];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →␈↓ βhsave[fun;func[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhsave[args;arglist[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhsave[cont;function[ev1]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhceval␈↓β1␈↓α[] ]]

␈↓ ↓H␈↓αev1 <= λ[[ ] restore[args]; restore[func]; restore[cont]; cont[] ]]

␈↓ ↓H␈↓We␈α↔can␈α↔remove␈α↔more␈α↔control␈α↔structure␈α_from␈α↔the␈α↔evaluator␈α↔by␈α↔noting␈α↔that␈α_executing␈α↔the
␈↓ ↓H␈↓continuation,␈α∩"␈↓αcont[]␈↓",␈α∩and␈α∩executing␈α∩the␈α∩explicit␈α⊃calls␈α∩on␈α∩the␈α∩evaluator's␈α∩subfunctions␈α∩are␈α⊃two
␈↓ ↓H␈↓manifestations␈α
of␈α
the␈αsame␈α
phenomenon.␈α
 In␈αthe␈α
first␈α
case␈αwe␈α
restore␈α
to␈αa␈α
variable␈α
and␈αthen␈α
execute
␈↓ ↓H␈↓the␈α
variable␈αas␈α
a␈α
function␈αapplication;␈α
in␈α
the␈αsecond,␈α
we␈α
execute␈αa␈α
known␈α
call.␈αWe␈α
can␈αreplace␈α
these
␈↓ ↓H␈↓two␈αactions␈αby␈αa␈αcommon␈αaction␈αif␈αwe␈αalways␈αexecute␈αfrom␈αthe␈αvariable␈α␈↓αcont␈↓␈αand␈αreplace␈αcalls␈αlike
␈↓ ↓H␈↓"␈↓αceval␈↓β1␈↓α[]␈↓" with the sequence:

␈↓ ↓H␈↓α␈↓ ∧ysave[cont;function[ceval␈↓β1␈↓α]]; cont[];

␈↓ ↓H␈↓Notice␈αthat␈αwhen␈αwe␈αmake␈αthis␈αlast␈α␈↓αsave␈↓␈αwe␈αknow␈αthat␈αthe␈αcurrent␈αvalue␈αof␈α␈↓αcont␈↓␈αis␈α␈↓αev1␈↓.␈αNotice␈αalso
␈↓ ↓H␈↓that␈αwhen␈αwe␈αexecute␈α␈↓αcont[]␈↓␈αwe␈αenter␈α␈↓αceval␈↓β1␈↓␈αand␈αtherefore␈αwithin␈αthis␈αcall␈αon␈α␈↓αceval␈↓β1␈↓,␈α␈↓αcont␈↓␈α␈↓↓is␈↓␈α␈↓αceval␈↓β1␈↓.
␈↓ ↓H␈↓All␈αthis␈αdiscussion␈αcan␈αbe␈αsimplified␈αif␈αwe␈α
think␈αa␈αbit␈αabout␈αthe␈αpurpose␈αof␈αcontinuations:␈α
we␈αwill
␈↓ ↓H␈↓need␈α
to␈α
make␈α
note␈α
of␈α
what␈α
the␈α∞continuation␈α
should␈α
be␈α
␈↓↓after␈↓␈α
the␈α
current␈α
computation␈α∞is␈α
finished;
␈↓ ↓H␈↓␈↓↓158  Evaluation␈↓ %4.18␈↓


␈↓ ↓H␈↓and␈α
we␈αwill␈α
need␈αto␈α
set␈α
␈↓αcont␈↓␈αto␈α
designate␈αwhat␈α
computation␈α
to␈αdo␈α
now.␈α We␈α
therefore␈α
introduce␈αa
␈↓ ↓H␈↓binary␈αprimitive␈α␈↓αsave_cont␈↓␈αwhich␈αwill␈αsave␈αits␈αfirst␈αargument␈αsuch␈αthat␈α␈↓αrestore␈↓␈αcan␈αrestore␈αit␈αto␈α␈↓αcont␈↓
␈↓ ↓H␈↓at the appropriate time; ␈↓αsave_cont␈↓ will set ␈↓αcont␈↓ from its second argument.

␈↓ ↓H␈↓α␈↓ ∧@save_cont <= λ[[x;y] cont ← x; save[cont;y]]

␈↓ ↓H␈↓We can remove the calls ␈↓αcont[]␈↓, and perform the execution outside ␈↓αceval␈↓λ'␈↓ using a simple loop:

␈↓ ↓H␈↓α␈↓ ¬_loop <= λ[[]prog[[]
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ε_l␈↓ ε8cont[]
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ε_␈↓ ε8go[l] ]]

␈↓ ↓H␈↓Each␈α
function␈αexecuted␈α
by␈α␈↓αcont[]␈↓␈α
will␈α
perform␈αsome␈α
simple␈αoperations␈α
like␈α␈↓αsend␈↓␈α
or␈α
␈↓αalloc_dest␈↓,␈αand
␈↓ ↓H␈↓then␈α
will␈α
exit,␈α
setting␈α␈↓αcont␈↓␈α
to␈α
a␈α
function␈αname.␈α
The␈α
next␈α
pass␈α
around,␈α␈↓αloop␈↓␈α
will␈α
execute␈α
the␈αnew␈α
␈↓αcont␈↓.
␈↓ ↓H␈↓After slight reorganization to eliminate some ␈↓αsave-restore␈↓ operations on ␈↓αcont␈↓, we have:

␈↓ ↓H␈↓αceval␈↓λ''␈↓α <= λ[[ ]␈↓ β([isconst[] → send[denote[]]; restore[cont];
␈↓ ↓H␈↓α␈↓ β( isvar[] → send[lookup[]]; restore[cont];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →␈↓ βhsave[fun;func[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhsave[args;arglist[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βhsave_cont[quote[ev1];quote[ceval␈↓β1␈↓α]] ]]

␈↓ ↓H␈↓αev1 <= λ[[ ] restore[args]; restore[func]; restore[cont] ]]

␈↓ ↓H␈↓Notice␈αthe␈αuse␈αof␈α␈↓αquote␈↓␈αrather␈αthan␈α␈↓αfunction␈↓.␈αIn␈αthe␈αprevious␈αevaluators␈αwe␈αused␈α␈↓αfunction␈↓␈αsince␈αwe
␈↓ ↓H␈↓had␈αto␈αsave␈αthe␈αcurrent␈αenvironment;␈αbut␈αthe␈αcontinuation␈α␈↓αc␈↓␈αwas␈αthe␈αonly␈αfree␈αvariable␈αwhich␈αwas
␈↓ ↓H␈↓in␈α∞jeopardy.␈α
In␈α∞␈↓αceval␈↓λ''␈↓␈α
we␈α∞have␈α
explicitly␈α∞saved␈α
the␈α∞continuation␈α
using␈α∞␈↓αsave_cont␈↓,␈α
and␈α∞thus␈α
␈↓αquote␈↓
␈↓ ↓H␈↓plus␈αthe␈αproper␈αuse␈αof␈α␈↓αrestore␈↓␈αcan␈αreplace␈α␈↓αfunction␈↓.␈αWe␈αwill␈αalso␈αintroduce␈αan␈αabbreviation,␈αwriting
␈↓ ↓H␈↓␈↓λ`␈↓αxx␈↓ for ␈↓αquote[xx]␈↓␈↓π 82␈↓.

␈↓ ↓H␈↓α␈↓Finally, here's ␈↓αeval␈↓:

␈↓ ↓H␈↓α␈↓ β(eval <= λ[[x;y]catch[␈↓ ¬8prog[[ ][␈↓ ε(fun ← ();
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬8␈↓ ε(args ← ();
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬8␈↓ ε(exp ← x;
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬8␈↓ ε(env ← y;
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬8␈↓ ε(dest ← alloc_dest[(TLB)];
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬8␈↓ ε(save_cont[␈↓λ`␈↓αλ[[]throw[val[dest]; out]]]; ␈↓λ`␈↓αceval␈↓λ''␈↓α];
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬8␈↓ ε(loop[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ¬8out]]

␈↓ ↓H␈↓What␈α∀has␈α∀been␈α∀gained␈α∃by␈α∀these␈α∀transformations␈α∀of␈α∀the␈α∃original␈α∀␈↓αeval␈↓?␈α∀ We␈α∀have␈α∃made␈α∀the

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 82␈↓ This abbreviation is used in several implementations of LISP. See page 214.
␈↓ ↓H␈↓␈↓↓4.18␈↓ λ:␈↓αeval␈↓ with explicit control     159␈↓α


␈↓ ↓H␈↓mechanisms␈αwhich␈αwere␈αimplicit␈αin␈αLISP␈αvery␈αexplicit.␈α We␈αhave␈αdescribed␈αthe␈αimplementations␈αof
␈↓ ↓H␈↓LISP's␈αaccess␈αand␈αcontrol␈αrequirements␈αin␈αterms␈αof␈αvery␈αsimple␈αcomputations.␈αIndeed,␈αwe␈αnow␈αhave
␈↓ ↓H␈↓developed␈α∂enough␈α∂detail␈α∂that␈α⊂we␈α∂can␈α∂give␈α∂a␈α∂faithful␈α⊂implementation␈α∂description␈α∂of␈α∂all␈α⊂of␈α∂LISP
␈↓ ↓H␈↓including the ␈↓αfunction␈↓ and ␈↓αprog␈↓ constructs.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓

␈↓ ↓H␈↓␈↓ α"I. Could we use statements like ␈↓αsave_cont[ev1;eval␈↓β1␈↓α]␈↓ rather than ␈↓αsave_cont[␈↓λ`␈↓αev1; ␈↓λ`␈↓αeval␈↓β1␈↓α]␈↓?

␈↓ ↓H␈↓␈↓ α5II. Using the new evaluator, sketch the evaluation of ␈↓αf[A]␈↓ where: ␈↓αf <= λ[[x]eq[x;A]]␈↓.



␈↓ ↓H␈↓␈↓ ¬≠␈↓↓4.19  An evaluator for ␈↓αprog␈↓␈↓α


␈↓ ↓H␈↓The␈αevaluator␈αin␈αthis␈αsection␈αwill␈αbe␈αthe␈αdefinitive␈αinterpreter␈αfor␈αLISP␈αthroughout␈αthe␈αrest␈αof␈αthis
␈↓ ↓H␈↓book. It will handle the applicative subset of LISP as well as handling ␈↓αprog␈↓s and ␈↓αfunction␈↓.

␈↓ ↓H␈↓We␈α∩need␈α∪to␈α∩add␈α∪more␈α∩mechanism␈α∩to␈α∪handle␈α∩␈↓αprog␈↓.␈α∪For␈α∩example␈α∩the␈α∪execution␈α∩of␈α∪the␈α∩␈↓αreturn␈↓
␈↓ ↓H␈↓statement␈α∂requires␈α∂that␈α∞we␈α∂locate␈α∂the␈α∂dynamically␈α∞surrounding␈α∂␈↓αprog␈↓.␈α∂The␈α∞␈↓αgo␈↓␈α∂must␈α∂also␈α∂locate␈α∞its
␈↓ ↓H␈↓surrounding␈α∂␈↓αprog␈↓␈α∞which␈α∂contains␈α∂the␈α∞desired␈α∂label.␈α∂ The␈α∞evaluator␈α∂needs␈α∂to␈α∞know␈α∂when␈α∂we␈α∞are
␈↓ ↓H␈↓evaluating␈αa␈αconditional␈αexpression␈αand␈αwhen␈αwe␈αare␈αevaluating␈αa␈αconditional␈αstatement;␈αif␈αwe␈αare
␈↓ ↓H␈↓(immediately)␈αin␈αa␈α␈↓αprog␈↓␈αthen␈αits␈αa␈αconditional␈αstatement,␈αotherwise␈αits␈αa␈αconditional␈αexpression.␈α All
␈↓ ↓H␈↓of␈αthis␈αinformation␈αcould␈αbe␈αdiscovered␈αby␈αthe␈αevaluator␈αusing␈αthe␈αcurrently␈αsupplied␈αinformation,
␈↓ ↓H␈↓however␈αthe␈αevaluator␈αcan␈αbe␈αmade␈αmore␈α
efficient␈αby␈αadding␈αa␈αbit␈αmore␈αexplicit␈αinformation.␈α
Most
␈↓ ↓H␈↓of␈αthe␈αadditions␈α
involve␈α␈↓αprog␈↓-entry,␈α␈↓αgo␈↓,␈α
and␈α␈↓αreturn␈↓␈αand␈α
will␈αtherefore␈αbe␈α
presented␈αwhen␈αwe␈α
discuss
␈↓ ↓H␈↓that␈αpart␈αof␈αthe␈αevaluator.␈αThe␈αonly␈αaddition␈αwe␈αwill␈αmake␈αnow␈αwill␈αbe␈αintroduction␈αof␈αa␈αvariable
␈↓ ↓H␈↓␈↓αtype␈↓␈α∞which␈α
is␈α∞set␈α
to␈α∞␈↓αPROC␈↓␈α
when␈α∞we␈α∞begin␈α
an␈α∞evaluation␈α
of␈α∞an␈α
application,␈α∞and␈α
is␈α∞set␈α∞to␈α
␈↓αPROG␈↓
␈↓ ↓H␈↓when we enter a ␈↓αprog␈↓.

␈↓ ↓H␈↓We␈α∀will␈α∪also␈α∀rework␈α∪some␈α∀of␈α∪our␈α∀current␈α∪sub-functions␈α∀to␈α∪aid␈α∀clarity␈α∪and␈α∀efficiency.␈α∪ Since
␈↓ ↓H␈↓sequences␈αof␈α␈↓αsave␈↓s␈αhappen␈αfrequently␈αin␈αthe␈αevaluators␈αwe␈αintroduce␈αa␈αnew␈αprocedure␈αnamed␈α␈↓αsave␈↓λ'␈↓
␈↓ ↓H␈↓which␈αacts␈αlike␈αa␈αsequence␈αof␈αcalls␈αon␈α␈↓αsave␈↓␈αfor␈αarguments␈αother␈αthan␈α␈↓αcont␈↓.␈α In␈αthis␈αlatter␈αcase,␈αa␈αcall
␈↓ ↓H␈↓on ␈↓αsave_cont␈↓ is simulated.  Similarly we introduce an iterated version of ␈↓αrestore␈↓ named ␈↓αrestore␈↓λ'␈↓.

␈↓ ↓H␈↓αpeval <= λ[[ ]␈↓ β_[isconst[] → send[denote[]];restore[cont];
␈↓ ↓H␈↓α␈↓ β_ isvar[] → send[lookup[]];restore[cont];
␈↓ ↓H␈↓α␈↓ β_ ␈↓
t␈↓α →␈↓ βhsave␈↓λ'␈↓α[␈↓ ∧8fun;func[];
␈↓ ↓H␈↓α␈↓ β_␈↓ βh␈↓ ∧8args;arglist[];
␈↓ ↓H␈↓α␈↓ β_␈↓ βh␈↓ ∧8cont; ␈↓λ`␈↓αev1; ␈↓λ`␈↓αpeval␈↓β1␈↓α] ]]

␈↓ ↓H␈↓αev1 <= λ[[ ] restore␈↓λ'␈↓α[args;func;cont] ]]
␈↓ ↓H␈↓␈↓↓160  Evaluation␈↓ &4.19␈↓


␈↓ ↓H␈↓The␈α⊂responsibility␈α⊂of␈α⊃␈↓αpeval␈↓␈α⊂is␈α⊂simply␈α⊃to␈α⊂recognize␈α⊂the␈α⊃occurrence␈α⊂of␈α⊂one␈α⊃of␈α⊂the␈α⊂basic␈α⊃forms:␈α⊂a
␈↓ ↓H␈↓variable,␈α
a␈α
constant,␈α
or␈α
a␈α
function␈α∞application.␈α
 Discovering␈α
the␈α
structure␈α
of␈α
an␈α
application␈α∞is␈α
the
␈↓ ↓H␈↓business␈α⊂of␈α⊂␈↓αpeval␈↓β1␈↓.␈α⊃ We␈α⊂need␈α⊂to␈α⊃know␈α⊂whether␈α⊂the␈α⊃function␈α⊂position␈α⊂represents␈α⊃a␈α⊂call-by-value
␈↓ ↓H␈↓function␈αor␈αa␈α
special␈αform.␈αSo␈α
far␈αthe␈αonly␈αspecial␈α
form␈αwe␈αrecognize␈α
is␈α␈↓αcond␈↓␈↓π 83␈↓;␈αhowever␈α
many␈αof
␈↓ ↓H␈↓the␈α∞constructs␈α
which␈α∞␈↓αprog␈↓␈α
introduced␈α∞are␈α∞special␈α
forms.␈α∞We␈α
could␈α∞add␈α
a␈α∞collection␈α∞of␈α
recognizers
␈↓ ↓H␈↓␈↓αissetq␈↓,␈α
␈↓αisgo␈↓,␈α
␈↓αisprog␈↓,␈α
etc.,␈α
to␈α
augment␈α
the␈αexisting␈α
␈↓αiscond␈↓.␈α
Instead␈α
we␈α
would␈α
rather␈α
add␈α
a␈αdevice␈α
similar
␈↓ ↓H␈↓to␈α∞␈↓αisprim␈↓␈α∞but␈α∞instead␈α∂of␈α∞handling␈α∞the␈α∞call-by-value␈α∂primitives␈α∞with␈α∞the␈α∞underlying␈α∂evaluator,␈α∞we
␈↓ ↓H␈↓wish␈α
to␈α
handle␈α
special␈α
forms␈αwith␈α
our␈α
own␈α
pieces␈α
of␈α
␈↓αpeval␈↓.␈αWe␈α
need␈α
a␈α
predicate␈α
named␈α
␈↓αisspec␈↓␈αto
␈↓ ↓H␈↓recognize␈α⊃occurrences␈α⊃of␈α⊂special␈α⊃forms␈α⊃and␈α⊃will␈α⊂need␈α⊃to␈α⊃add␈α⊃functions␈α⊂to␈α⊃␈↓αpeval␈↓␈α⊃to␈α⊃execute␈α⊂the
␈↓ ↓H␈↓appropriate␈α∂programs␈α∂when␈α∂␈↓αisspec␈↓␈α∂is␈α∂true.␈α∂We␈α⊂will␈α∂do␈α∂this␈α∂by␈α∂introducing␈α∂a␈α∂global␈α⊂table␈α∂called
␈↓ ↓H␈↓␈↓αspectbl␈↓.␈α∀The␈α∀name␈α∀components␈α∀of␈α∪the␈α∀table␈α∀will␈α∀be␈α∀the␈α∪names␈α∀for␈α∀special␈α∀forms;␈α∀the␈α∪value
␈↓ ↓H␈↓components␈αof␈α␈↓αspectbl␈↓␈αwill␈αbe␈αthe␈αnames␈αof␈αfunctions␈αwhich␈αwill␈αevaluate␈αthe␈αcorresponding␈αspecial
␈↓ ↓H␈↓form␈↓π 84␈↓.  Then we can write:

␈↓ ↓H␈↓α␈↓ β(isspec <= λ[[ ][null[nassoc[fun;spectbl]] → ␈↓
f␈↓α; ␈↓
t␈↓α → ␈↓
t␈↓α]

␈↓ ↓H␈↓α␈↓ β(nassoc <= λ[[x;l][null[l] → ( ); ␈↓
t␈↓α → assoc[x;l]]

␈↓ ↓H␈↓To␈α
execute␈α
the␈α
appropriate␈α
routine␈α
we␈α
need␈α
only␈α
put␈α
the␈α
name␈α
in␈α
the␈α
variable␈α
␈↓αcont␈↓␈α
and␈α
␈↓αloop␈↓␈αwill␈α
do
␈↓ ↓H␈↓the rest. We can load ␈↓αcont␈↓ by:

␈↓ ↓H␈↓α␈↓ β∀cont ← valspec[ ]; ␈↓where:    ␈↓αvalspec <= λ[[ ]value[nassoc[fun;spectbl]]]

␈↓ ↓H␈↓For␈α
example,␈αwith␈α
␈↓αspectbl␈↓␈α
bound␈αto␈α
␈↓α((COND␈αDEVCOND))␈↓,␈α
our␈α
previous␈α␈↓αceval␈↓β1␈↓␈α
would␈α
work␈αquite
␈↓ ↓H␈↓nicely as:

␈↓ ↓H␈↓αceval␈↓β1␈↓α <= λ[[ ]␈↓ β_[isatom[] →␈↓ ∧([isspec[] → cont ← valspec[ ];
␈↓ ↓H␈↓α␈↓ β_␈↓ ∧( isprim[] →␈↓ ¬hsave[env;env];
␈↓ ↓H␈↓α␈↓ β_␈↓ ∧(   ...  ]  ... ]]

␈↓ ↓H␈↓Before␈αintroducing␈α␈↓αpeval␈↓β1␈↓␈αwe␈αshould␈αsay␈αa␈αbit␈αabout␈αthe␈αinefficiency␈αinvolved␈αin␈αthe␈α␈↓αisspec␈↓-␈↓αvalspec␈↓
␈↓ ↓H␈↓pair.␈α∀We␈α∃already␈α∀noted␈α∀that␈α∃the␈α∀linear␈α∃search␈α∀encoded␈α∀in␈α∃␈↓αassoc␈↓␈α∀is␈α∃unnecessarily␈α∀inefficient.
␈↓ ↓H␈↓However␈α∞the␈α∂present␈α∞predicate-function␈α∂pair␈α∞is␈α∂even␈α∞more␈α∞wasteful;␈α∂if␈α∞␈↓αisspec␈↓␈α∂␈↓↓is␈↓␈α∞true␈α∂we␈α∞perform
␈↓ ↓H␈↓␈↓αnassoc[fun;spectbl]␈↓␈α∞twice.␈α
 A␈α∞more␈α
efficient␈α∞computation␈α∞would␈α
save␈α∞the␈α
result␈α∞of␈α
the␈α∞first␈α∞call␈α
on
␈↓ ↓H␈↓␈↓αnassoc␈↓ in a temporary variable ␈↓αt1␈↓ and if ␈↓αisspec␈↓ is true, move the ␈↓αvalue␈↓-part of ␈↓αt1␈↓ to ␈↓αcont␈↓. Thus:

␈↓ ↓H␈↓α␈↓ β(isspec <= λ[[ ]␈↓ ∧xt1 ← assoc[fun;spectbl]]
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧x[null[t1] → ␈↓
f␈↓α; ␈↓
t␈↓α → ␈↓
t␈↓α] ]

␈↓ ↓H␈↓α␈↓with:␈↓ β(␈↓αvalspec <= λ[[ ]value[t1]]

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 83␈↓␈α∞Actually␈α∂␈↓αquote␈↓␈α∞is␈α∂also␈α∞a␈α∂special␈α∞form␈α∂which␈α∞we␈α∂recognize,␈α∞however␈α∂its␈α∞recognition␈α∂is␈α∞handled
␈↓ ↓H␈↓within ␈↓αisconst␈↓.

␈↓ ↓H␈↓␈↓π 84␈↓ In the next chapter we will see a more efficient way to recognize and execute special forms.
␈↓ ↓H␈↓␈↓↓4.19␈↓ λVAn evaluator for ␈↓αprog␈↓     161␈↓α


␈↓ ↓H␈↓This␈αis␈α
a␈αuseful␈αprogramming␈α
trick␈αbut␈αdoes␈α
not␈αadd␈αto␈α
the␈αclarity␈αof␈α
the␈αprogram.␈α
In␈αSection 5.4
␈↓ ↓H␈↓we shall see a more subtle, but related trick.

␈↓ ↓H␈↓What follows is the remainder of the evaluator interspersed with commentary.

␈↓ ↓H␈↓The␈α∀main␈α∀function␈α∀is␈α∃␈↓αpeval␈↓β1␈↓;␈α∀it␈α∀handles␈α∀function␈α∃applications.␈α∀ The␈α∀application␈α∀is␈α∃either␈α∀a
␈↓ ↓H␈↓call-by-value␈αapplication␈αor␈αit␈αis␈αa␈αspecial␈αform.␈αAn␈αinstance␈αof␈αthe␈αfirst␈αrequires␈αevaluation␈αof␈αthe
␈↓ ↓H␈↓argument␈αlist␈αand␈αthen␈αevaluation␈αof␈αthe␈αprocedure␈αbody.␈αIf␈αthe␈αapplication␈αis␈αa␈αspecial␈αform␈αthen
␈↓ ↓H␈↓the␈α
evaluation␈α
is␈α
handled␈α
by␈α
a␈α
special␈αpiece␈α
of␈α
the␈α
evaluator,␈α
using␈α
the␈α
mechanism␈αdescribed␈α
above.
␈↓ ↓H␈↓The␈α∞call-by-value␈α∞applications␈α∞are␈α
either␈α∞primitive␈α∞applications␈α∞or␈α
are␈α∞anonymous␈α∞λ-terms.␈α∞If␈α
the
␈↓ ↓H␈↓form␈α
is␈α
not␈αrecognizable␈α
then␈α
the␈α
function-position␈αis␈α
evaluated␈α
and␈αthen␈α
applied␈α
to␈α
its␈αargument
␈↓ ↓H␈↓list.␈α The␈αanomalous␈αsituation␈αinvolves␈αthe␈αapplication␈αof␈αa␈α␈↓αfunarg␈↓;␈αthough␈αit␈αis␈αpossible␈αto␈αhandle
␈↓ ↓H␈↓this case as a primitive, it is more instructive to present it in detail. Here is ␈↓αpeval␈↓β1␈↓:

␈↓ ↓H␈↓␈↓αpeval␈↓β1␈↓α <= λ[[ ]␈↓ β([isatom[] →␈↓ ∧X[isspec[] →   cont ← valspec[ ];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X isprim[] →␈↓ ¬hsave␈↓λ'␈↓α[␈↓ ε8env;env;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬h␈↓ ε8dest;alloc_dest[createvars[]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬h␈↓ ε8cont; ␈↓λ`␈↓αev2; ␈↓λ`␈↓αevalargs];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X ␈↓
t␈↓α → save␈↓λ'␈↓α[exp;fun;cont; ␈↓λ`␈↓αev3; ␈↓λ`␈↓αpeval];
␈↓ ↓H␈↓α␈↓ β(islambda[] →␈↓ ∧Xsave␈↓λ'␈↓α[␈↓ ¬(env;env;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(dest;alloc_dest[vars[];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(cont; ␈↓λ`␈↓αev5; ␈↓λ`␈↓αevalargs];
␈↓ ↓H␈↓α␈↓ β(isfunarg[] →␈↓ ∧Xprog[[x;y]␈↓ ¬hx ← args;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬hargs ← bodylist[second[fun]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬hy ← env;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬henv ← third[fun];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬hsave␈↓λ'␈↓α[␈↓ ε8env;env;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬h␈↓ ε8args;x;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬h␈↓ ε8dest;alloc_dest[vars[second[fun]]];
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬h␈↓ ε8env;y;
␈↓ ↓H␈↓α␈↓ β(␈↓ ∧X␈↓ ¬(␈↓ ¬h␈↓ ε8cont; ␈↓λ`␈↓αev7; ␈↓λ`␈↓αevalargs]];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → save␈↓λ'␈↓α[exp;fun; cont; ␈↓λ`␈↓αev3; ␈↓λ`␈↓αpeval] ]]

␈↓ ↓H␈↓The functions ␈↓αev2␈↓ through ␈↓αev8␈↓ handle the control in ␈↓αpeval␈↓β1␈↓:

␈↓ ↓H␈↓α␈↓ βLev2 <= λ[[ ] link[]; restore[dest]; execute[]; restore␈↓λ'␈↓α[env;cont]]


␈↓ ↓H␈↓α␈↓This function passes the evaluation to the body of the primitive.
␈↓ ↓H␈↓␈↓↓162  Evaluation␈↓ &4.19␈↓




␈↓ ↓H␈↓␈↓ β7␈↓αev3 <= λ[[ ] restore[exp]; save␈↓λ'␈↓α[fun;receive[];cont; ␈↓λ`␈↓αev4; ␈↓λ`␈↓αpeval␈↓β1␈↓α]]

␈↓ ↓H␈↓␈↓αev3␈↓␈α
is␈α
the␈α
return␈α
point␈α∞when␈α
we␈α
have␈α
to␈α
evaluate␈α
the␈α∞function␈α
position␈α
of␈α
a␈α
form.␈α
 When␈α∞␈↓αev3␈↓␈α
is
␈↓ ↓H␈↓called␈αthe␈α
result␈αof␈α
that␈αevaluation␈α
is␈αin␈αthe␈α
current␈αdest-slot.␈α
 A␈α␈↓αreceive␈↓␈α
gets␈αthe␈α
value;␈αwe␈αthen␈α
pass
␈↓ ↓H␈↓the new form back to ␈↓αpeval␈↓β1␈↓.

␈↓ ↓H␈↓α␈↓ ¬∞ev4 <= λ[[ ] restore␈↓λ'␈↓α[fun;cont]]

␈↓ ↓H␈↓α␈↓ αlev5 <= λ[[ ] link[]; restore[dest]; save␈↓λ'␈↓α[args;bodylist[]; cont; ␈↓λ`␈↓αev6; ␈↓λ`␈↓αevalargs]]

␈↓ ↓H␈↓␈↓αev5␈↓␈α∞handles␈α
the␈α∞evaluation␈α∞of␈α
the␈α∞body␈α∞of␈α
a␈α∞λ-expression.␈α∞Since␈α
we␈α∞are␈α∞allowing␈α
multiple-bodied
␈↓ ↓H␈↓λ-expressions␈α∪(page 146),␈α∪we␈α∪pass␈α∪the␈α∪␈↓αbodylist␈↓␈α∪to␈α∪␈↓αevalargs␈↓.␈α∪If␈α∪we␈α∪were␈α∪restricting␈α∪ourselves␈α∩to
␈↓ ↓H␈↓single-bodied expressions, then passing ␈↓αbody␈↓ to ␈↓αpeval␈↓ would suffice.

␈↓ ↓H␈↓α␈↓ ∧sev6 <= λ[[ ]restore␈↓λ'␈↓α[args;env;cont]]

␈↓ ↓H␈↓The functions ␈↓αev7␈↓ and ␈↓αev8␈↓ control the application of a ␈↓αfunarg␈↓.

␈↓ ↓H␈↓αev7 <= λ[[ ]␈↓ αxrestore[env];
␈↓ ↓H␈↓α␈↓ αxlink[];
␈↓ ↓H␈↓α␈↓ αxrestore␈↓λ'␈↓α[dest;args];
␈↓ ↓H␈↓α␈↓ αxsave_cont[ ␈↓λ`␈↓αev8; ␈↓λ`␈↓αevalargs] ]]

␈↓ ↓H␈↓αev8 <= λ[[ ] restore␈↓λ'␈↓α[env;cont]]

␈↓ ↓H␈↓The␈α∞next␈α∞four␈α∞functions␈α∂handle␈α∞the␈α∞evaluation␈α∞of␈α∞a␈α∂sequence␈α∞of␈α∞expressions.␈α∞ If␈α∞the␈α∂sequence␈α∞is
␈↓ ↓H␈↓empty,␈α∞then␈α∞there␈α∞is␈α∞noting␈α∞to␈α∞do.␈α∞If␈α∂there␈α∞is␈α∞a␈α∞single␈α∞argument␈α∞then␈α∞evaluate␈α∞it␈α∞and␈α∂restore␈α∞the
␈↓ ↓H␈↓continuation.␈α⊂Otherwise␈α⊂evaluate␈α∂the␈α⊂first␈α⊂one␈α⊂using␈α∂␈↓αpeval␈↓␈α⊂(sending␈α⊂its␈α∂result␈α⊂to␈α⊂␈↓αdest␈↓)␈α⊂and␈α∂then
␈↓ ↓H␈↓execute␈α
␈↓αev11␈↓.␈α At␈α
␈↓αev11␈↓␈αwe␈α
update␈αthe␈α
destination␈αblock␈α
using␈α␈↓αnext␈↓␈α
and␈αget␈α
set␈αto␈α
evaluate␈α
the␈αnext
␈↓ ↓H␈↓argument.

␈↓ ↓H␈↓αevalargs <= λ[[]␈↓ β([emptyargs[] → restore[cont];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →␈↓ βxsave[exp;first[args]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxcont ← ␈↓λ`␈↓αev9 ]]

␈↓ ↓H␈↓αev9 <= λ[[ ]␈↓ αh[singlearg[] →␈↓ ∧(save_cont[ ␈↓λ`␈↓αev10; ␈↓λ`␈↓αpeval];
␈↓ ↓H␈↓α␈↓ αh ␈↓
t␈↓α →␈↓ β8save_cont[ ␈↓λ`␈↓αev11; ␈↓λ`␈↓αpeval]  ]]

␈↓ ↓H␈↓αev10 <= λ[[ ] restore␈↓λ'␈↓α[exp;cont]]

␈↓ ↓H␈↓αe11 <= λ[[ ]␈↓ αhnext[];
␈↓ ↓H␈↓α␈↓ αhargs ← rest[args];
␈↓ ↓H␈↓α␈↓ αhexp ← first[args];
␈↓ ↓H␈↓α␈↓ αhcont ← ␈↓λ`␈↓αev9 ] ]
␈↓ ↓H␈↓␈↓↓4.19␈↓ λVAn evaluator for ␈↓αprog␈↓     163␈↓α


␈↓ ↓H␈↓␈↓ ε↔␈↓↓Problem␈↓

␈↓ ↓H␈↓␈↓ αHUsing the new evaluator, sketch the evaluation of ␈↓αf[A]␈↓ where: ␈↓αf <= λ[[x]eq[x;A]]␈↓.

␈↓ ↓H␈↓The␈α∪combination␈α∪of␈α∀␈↓αevcond␈↓␈α∪and␈α∪␈↓αcond1␈↓␈α∪handle␈α∀conditional␈α∪expressions␈↓π 85␈↓.␈α∪ ␈↓αevcond␈↓␈α∪sets␈α∀up␈α∪the
␈↓ ↓H␈↓evaluation␈α
of␈α
the␈α
predicate␈α∞position␈α
such␈α
that␈α
the␈α
computation␈α∞will␈α
continue␈α
at␈α
␈↓αcond1␈↓.␈α∞When␈α
that
␈↓ ↓H␈↓evaluation␈α
is␈α
completed␈α
␈↓αcond1␈↓␈α
␈↓αreceive␈↓s␈α
the␈α
result.␈α
If␈α
␈↓
t␈↓␈α
is␈α
received␈α
then␈α
the␈α
consequent␈α
part␈α∞of␈α
that
␈↓ ↓H␈↓conditional␈α
clause␈α
evaluated.␈α
Note␈αthat␈α
we␈α
use␈α
␈↓αevalargs␈↓␈αhere␈α
since␈α
we␈α
allow␈α
extended␈αconditionals
␈↓ ↓H␈↓(page 143).  If ␈↓
f␈↓ is received we go back to ␈↓αevcond␈↓ with the remaining part of the conditional.

␈↓ ↓H␈↓αevcond <= λ[[ ]␈↓ β([emptyargs[] →␈↓ ∧herr[NO_TRUE_COND_CLAUSE];
␈↓ ↓H␈↓α␈↓ β(␈↓ βx␈↓ ∧hcont ← ␈↓λ`␈↓αev1;
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →␈↓ βxsave_cont[ ␈↓λ`␈↓αcond1; ␈↓λ`␈↓αpeval];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxexp ← pred[first[args]] ]]

␈↓ ↓H␈↓αcond1 <= λ[[ ]␈↓ β([receive[] →␈↓ ∧Xargs ← conseq[first[args]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βx␈↓ ∧Xsave_cont[ ␈↓λ`␈↓αev1; ␈↓λ`␈↓αevalargs];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α →␈↓ βxargs ← rest[args];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxcont ← ␈↓λ`␈↓αevcond  ]]

␈↓ ↓H␈↓The␈α
next␈α
two␈α∞functions␈α
deal␈α
with␈α
functional␈α∞arguments.␈α
 If␈α
the␈α
argument␈α∞is␈α
a␈α
primitive,␈α∞then␈α
we
␈↓ ↓H␈↓just␈α␈↓αquote␈↓␈αit;␈αthe␈αassumption␈αis␈αthat␈α
primitives␈αonly␈αaccess␈αlocal␈αvariables␈αand␈αtherefore␈α
don't␈αneed
␈↓ ↓H␈↓to␈α⊂save␈α⊂the␈α⊂environment.␈α⊂An␈α⊂expression␈α⊂which␈α⊂is␈α∂already␈α⊂␈↓αfunarg␈↓-ed␈α⊂is␈α⊂passed␈α⊂as␈α⊂is.␈α⊂ If␈α⊂it␈α⊂is␈α∂a
␈↓ ↓H␈↓λ-expression, we make a ␈↓αfunarg␈↓; otherwise we evaluate the function until we discover its character.

␈↓ ↓H␈↓αevfunction <= λ[[ ]␈↓ βXfun ← first[args];
␈↓ ↓H␈↓α␈↓ βX[isprim[] → send[mkquote[fun]];restore[cont];
␈↓ ↓H␈↓α␈↓ βX islambda[] → send[mkfunarg[fun;env]];restore[cont];
␈↓ ↓H␈↓α␈↓ βX isfunarg[] → send[fun];restore[cont];
␈↓ ↓H␈↓α␈↓ βX␈↓
t␈↓α →␈↓ ∧8save_cont[ ␈↓λ`␈↓αfun1; ␈↓λ`␈↓αpeval];
␈↓ ↓H␈↓α␈↓ βX␈↓ ∧8exp ← fun ]]

␈↓ ↓H␈↓αfun1 <= λ[[ ] send[mkfun[receive[ ]]; cont ← ␈↓λ`␈↓αev1]











␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 85␈↓ See the problem on page 167.
␈↓ ↓H␈↓␈↓↓164  Evaluation␈↓ &4.19␈↓




␈↓ ↓H␈↓Special␈α
functions␈α
are␈α
needed␈αto␈α
handle␈α
explicit␈α
calls␈αon␈α
the␈α
evaluator:␈α
␈↓αeval␈↓[<form>;<env>].␈α
We␈αset
␈↓ ↓H␈↓up␈α∞a␈α∞destination␈α∞to␈α∞receive␈α∂the␈α∞values␈α∞of␈α∞<form>␈α∞and␈α∂<env>,␈α∞and␈α∞ask␈α∞␈↓αevalargs␈↓␈α∞to␈α∂evaluate␈α∞these
␈↓ ↓H␈↓arguments.  The results of the computation are seen by ␈↓αev12␈↓; this function sets up the call on ␈↓αpeval␈↓.

␈↓ ↓H␈↓αeveval <= λ[[ ]␈↓ βλsave␈↓λ'␈↓α[␈↓ βXenv;env;
␈↓ ↓H␈↓α␈↓ βλ␈↓ βXdest;alloc_dest[createvars[(G1 G2)]];
␈↓ ↓H␈↓α␈↓ βλ␈↓ βXcont; ␈↓λ`␈↓αev12; ␈↓λ`␈↓αevalargs]]

␈↓ ↓H␈↓αev12 <= λ[[ ]␈↓ βλexp ← first_dest[];
␈↓ ↓H␈↓α␈↓ βλenv ← second_dest[];
␈↓ ↓H␈↓α␈↓ βλrestore[dest];
␈↓ ↓H␈↓α␈↓ βλsave_cont[ ␈↓λ`␈↓αev13; ␈↓λ`␈↓αpeval]]

␈↓ ↓H␈↓αev13 <= λ[[ ] restore␈↓λ'␈↓α[env;cont]]      (␈↓≡␈↓α ev8)

␈↓ ↓H␈↓There␈αis␈α
a␈αsecond␈αform␈α
of␈αcall␈αon␈α
␈↓αeval␈↓␈αwhich␈α
is␈αuseful.␈αIf␈α
we␈αwrite␈α␈↓αeval␈↓[<form>],␈α
then␈αthe␈α<form>␈α
is
␈↓ ↓H␈↓evaluated in the environment which exists at the point of call. See problem on page 167.

␈↓ ↓H␈↓The␈αremainder␈α
of␈αthe␈α
evaluator␈αinvolves␈α
the␈αsemantics␈αof␈α
␈↓αprog␈↓s.␈α Several␈α
new␈αideas␈α
are␈αinvolved.
␈↓ ↓H␈↓As␈α
we␈αdiscussed␈α
on␈α
page 159,␈αwe␈α
must␈α
be␈αable␈α
to␈α
determine␈αwhether␈α
or␈α
not␈αwe␈α
are␈αexecuting␈α
within
␈↓ ↓H␈↓a␈α␈↓αprog␈↓:␈α
we␈αintroduced␈α
␈↓αtype␈↓␈αto␈α
handle␈αthis.␈α
 Also␈αevery␈α
expression␈αor␈α
statement␈αin␈α
LISP␈αhas␈αa␈α
value.
␈↓ ↓H␈↓Since␈α
we␈α
are␈α
always␈α
␈↓αsend␈↓-ing␈α
values,␈α
we␈α
must␈αhave␈α
a␈α
destination␈α
to␈α
receive␈α
the␈α
values␈α
created␈αby
␈↓ ↓H␈↓␈↓αprog␈↓ statements:␈αwe␈αwill␈αintroduce␈αa␈αdummy␈αdestination␈αwhich␈αwill␈αalways␈αreceive␈αthe␈αvalue␈αof␈αany
␈↓ ↓H␈↓statement.␈α⊃This␈α∩destination␈α⊃is␈α⊃named␈α∩␈↓αbb␈↓,␈α⊃for␈α⊃"bit bucket".␈α∩ Finally,␈α⊃we␈α⊃must␈α∩handle␈α⊃assignment
␈↓ ↓H␈↓statements.␈αThe␈αinnovation␈αhere␈αis␈αthat␈αthe␈α␈↓αsend␈↓␈αgoes␈αto␈αsome␈αpre-existing␈αdestination␈αand␈αdestroys
␈↓ ↓H␈↓the␈αcurrent␈α
value:␈αwe␈αuse␈α
a␈αprimitive␈α␈↓αmkdest␈↓␈α
whose␈αeffect␈αis␈α
to␈αgenerate␈αa␈α
destination␈αpointer␈αto␈α
the
␈↓ ↓H␈↓slot␈α∂which␈α∂is␈α∂to␈α∂receive␈α∂the␈α∂value␈α∂of␈α∂the␈α∂right-hand-side␈α∂of␈α∂the␈α∂assignment.␈α∂ In␈α∂␈↓αevsetq␈↓␈α∂we␈α∂use␈α∞a
␈↓ ↓H␈↓function␈α␈↓αlookup␈↓λ'␈↓␈αwhich␈αis␈αsimilar␈αto␈α␈↓αlookup␈↓␈αexcept␈αthat␈αit␈αreturns␈αa␈αpointer␈αto␈αthe␈αslot␈αcontaining␈αa
␈↓ ↓H␈↓value, rather than returning the value in the slot.

␈↓ ↓H␈↓Here are  the evaluators for ␈↓αsetq␈↓ and ␈↓αset␈↓:

␈↓ ↓H␈↓αevsetq <= λ[[ ]␈↓ β_save␈↓λ'␈↓α[␈↓ βhdest;mkdest[lookup␈↓λ'␈↓α[first[args]]];
␈↓ ↓H␈↓α␈↓ β_␈↓ βhcont; ␈↓λ`␈↓αsetq1; ␈↓λ`␈↓αpeval];
␈↓ ↓H␈↓α␈↓ β_exp ← second[args]]

␈↓ ↓H␈↓αsetq1 <= λ[[ ]␈↓ β_prog[[x]
␈↓ ↓H␈↓α␈↓ β_␈↓ βhx ← receive[];
␈↓ ↓H␈↓α␈↓ β_␈↓ βhrestore[dest];
␈↓ ↓H␈↓α␈↓ β_␈↓ βhsend[x];
␈↓ ↓H␈↓α␈↓ β_␈↓ βhcont ← ␈↓λ`␈↓αev1 ]]
␈↓ ↓H␈↓␈↓↓4.19␈↓ λVAn evaluator for ␈↓αprog␈↓     165␈↓α



␈↓ ↓H␈↓αevset <= λ[[ ]␈↓ αxsave␈↓λ'␈↓α[args;args; cont; ␈↓λ`␈↓αset1; ␈↓λ`␈↓αpeval];
␈↓ ↓H␈↓α␈↓ αxexp ← first[args] ]

␈↓ ↓H␈↓αset1 <= λ[[ ]␈↓ αxrestore[args];
␈↓ ↓H␈↓α␈↓ αxargs ← mkass[receive[];rest[args]];
␈↓ ↓H␈↓α␈↓ αxcont ← ␈↓λ`␈↓αevsetq ]

␈↓ ↓H␈↓The␈α
␈↓αprog␈↓␈α
evaluator,␈α␈↓αevprog␈↓,␈α
must␈α
take␈α
cognizance␈αof␈α
all␈α
of␈α
the␈αcontrol␈α
structures␈α
which␈α
can␈αoccur
␈↓ ↓H␈↓within␈α
a␈α∞␈↓αprog␈↓.␈α
Besides␈α
ordinary␈α∞recursion,␈α
we␈α
can␈α∞have␈α
␈↓αgo␈↓s␈α
and␈α∞␈↓αreturn␈↓s.␈α
The␈α
␈↓αgo␈↓␈α∞must␈α
be␈α∞able␈α
to
␈↓ ↓H␈↓search␈α⊂the␈α⊂dynamic␈α⊂chain␈α⊂for␈α⊂the␈α⊂appropriate␈α⊂label,␈α⊂and␈α⊂the␈α⊂␈↓αreturn␈↓␈α⊂must␈α⊂find␈α⊂the␈α∂dynamically
␈↓ ↓H␈↓enclosing␈α␈↓αprog␈↓.␈α To␈αhandle␈αeither␈αof␈αthese␈αeventualities,␈αwe␈α␈↓αsave␈↓␈αsome␈αadditional␈αinformation␈αwhen
␈↓ ↓H␈↓we␈α∞enter␈α∞a␈α
␈↓αprog␈↓.␈α∞First␈α∞we␈α
save␈α∞the␈α∞current␈α
state␈α∞of␈α∞the␈α
computation;␈α∞this␈α∞will␈α
allow␈α∞the␈α∞␈↓αreturn␈↓␈α
to
␈↓ ↓H␈↓␈↓αrestore␈↓␈α⊃everything␈α⊃as␈α⊃it␈α⊃leaves␈α∩the␈α⊃␈↓αprog␈↓.␈α⊃ Next␈α⊃we␈α⊃make␈α⊃a␈α∩new␈α⊃␈↓αenv␈↓␈α⊃which␈α⊃has␈α⊃bound␈α∩all␈α⊃the
␈↓ ↓H␈↓␈↓αprog␈↓ variables␈α∞to␈α∂␈↓α( )␈↓.␈α∞ We␈α∂save␈α∞␈↓↓that␈↓␈α∂␈↓αenv␈↓,␈α∞since␈α∂a␈α∞non-local␈α∞␈↓αgo␈↓␈α∂will␈α∞want␈α∂to␈α∞restore␈α∂that␈α∞␈↓αenv␈↓␈α∂as␈α∞it
␈↓ ↓H␈↓returns␈αfor␈αexecution.␈αFinally␈αwe␈αcreate␈αa␈α␈↓αgolist␈↓␈αwhich␈αis␈αa␈αlist␈αof␈αall␈αpoints␈αin␈αthe␈α␈↓αprog␈↓␈αwhich␈αhave
␈↓ ↓H␈↓labels.␈αThis␈αconstruct␈αallows␈αus␈αto␈αdiscover␈αquickly␈αwhich␈αlabels␈αare␈αpresent␈αin␈αthe␈α␈↓αprog␈↓␈αand␈αwhere
␈↓ ↓H␈↓they are␈↓π 86␈↓. After all this is done we are ready to execute the first line of the ␈↓αprog␈↓ body.

␈↓ ↓H␈↓αevprog <= λ[[ ]␈↓ β(save␈↓λ'␈↓α[␈↓ βxexp;exp;
␈↓ ↓H␈↓α␈↓ β(␈↓ βxenv;env;
␈↓ ↓H␈↓α␈↓ β(␈↓ βxdest;alloc_dest[prog_vars[args]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxfun;fun;
␈↓ ↓H␈↓α␈↓ β(␈↓ βxargs;prog_body[args];
␈↓ ↓H␈↓α␈↓ β(␈↓ βxtype;PROG];
␈↓ ↓H␈↓α␈↓ β(link[];
␈↓ ↓H␈↓α␈↓ β(save␈↓λ'␈↓α[␈↓ βxenv;env;
␈↓ ↓H␈↓α␈↓ β(␈↓ βxgolist;mkgolist[args]];
␈↓ ↓H␈↓α␈↓ β(cont ← ␈↓λ`␈↓αline ]

␈↓ ↓H␈↓αmkgolist <= λ[[body] prog[z]
␈↓ ↓H␈↓α␈↓ βxa␈↓ ∧([null[body] → return[z];
␈↓ ↓H␈↓α␈↓ βx␈↓ ∧( islabel[first[body]] →   z ← concat[body;z] ];
␈↓ ↓H␈↓α␈↓ βx␈↓ ∧(body ← rest[body];
␈↓ ↓H␈↓α␈↓ βx␈↓ ∧(go[a] ]]








␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 86␈↓␈α
If␈αit␈α
weren't␈αfor␈α
the␈α
existence␈αof␈α
anonymous␈α␈↓αprog␈↓s␈α
and␈αfunction-modifying␈α
functions,␈α
we␈αcould
␈↓ ↓H␈↓put the responsibility of making the go-list on "<=".
␈↓ ↓H␈↓␈↓↓166  Evaluation␈↓ &4.19␈↓



␈↓ ↓H␈↓The␈α∞actual␈α∞execution␈α∞of␈α∞each␈α
line␈α∞of␈α∞a␈α∞␈↓αprog␈↓ body␈α∞is␈α∞controlled␈α
by␈α∞the␈α∞pair␈α∞␈↓αline␈↓␈α∞and␈α∞␈↓αline1␈↓.␈α
Their
␈↓ ↓H␈↓behavior␈α
is␈α
similar␈α
to␈α
that␈α
of␈α
the␈α
functions␈α
within␈α
␈↓αevalargs␈↓.␈α
␈↓αline␈↓␈α
examines␈α
the␈α
next␈α∞expression;␈α
if
␈↓ ↓H␈↓there␈α
is␈α∞no␈α
next␈α
statement,␈α∞we␈α
exit␈α
with␈α∞␈↓α( )␈↓␈α
using␈α∞␈↓αprog_exit␈↓;␈α
if␈α
the␈α∞next␈α
statement␈α
is␈α∞a␈α
label,␈α∞it␈α
is
␈↓ ↓H␈↓ignored; otherwise we set up to evaluate the expression, setting the destination to ␈↓αbb␈↓.


␈↓ ↓H␈↓αline <= λ[[ ]␈↓ αx[null[args] → prog_exit[( )];
␈↓ ↓H␈↓α␈↓ αx islabel[first[args]] →   args ← rest[args];
␈↓ ↓H␈↓α␈↓ αx ␈↓
t␈↓α →␈↓ ∧(exp ← first[args];
␈↓ ↓H␈↓α␈↓ αx␈↓ ∧(dest ← bb;
␈↓ ↓H␈↓α␈↓ αx␈↓ ∧(save_cont[ ␈↓λ`␈↓αline1; ␈↓λ`␈↓αpeval] ]]

␈↓ ↓H␈↓αline1 <= λ[[ ]␈↓ αxargs ← rest[args];
␈↓ ↓H␈↓α␈↓ αxcont ← ␈↓λ`␈↓αline ]

␈↓ ↓H␈↓Note␈αthat␈αwe␈α
don't␈αchange␈α␈↓αcont␈↓␈α
in␈α␈↓αline␈↓␈αwhen␈αwe␈α
see␈αa␈αlabel;␈α
we␈αjust␈αleave␈αit␈α
at␈α␈↓αline␈↓␈αand␈α
␈↓αloop␈↓␈αdoes
␈↓ ↓H␈↓the rest.

␈↓ ↓H␈↓We␈α⊂call␈α⊃␈↓αprog_exit␈↓␈α⊂to␈α⊃return␈α⊂␈↓α( )␈↓␈α⊂when␈α⊃the␈α⊂body␈α⊃of␈α⊂the␈α⊂␈↓αprog␈↓␈α⊃is␈α⊂empty.␈α⊃ Thus␈α⊂the␈α⊃discussion␈α⊂of
␈↓ ↓H␈↓␈↓αprog_exit␈↓␈αinvolves␈αthe␈αsemantics␈αof␈α␈↓αreturn␈↓.␈α Of␈αthe␈αtwo␈αcontrol␈αmechanisms,␈α␈↓αreturn␈↓␈αis␈αsimpler␈αthan
␈↓ ↓H␈↓␈↓αgo␈↓.␈α
 Recalling␈α
the␈α
discussion␈α
of␈α
␈↓αsave␈↓␈α
on␈α
page 152,␈α
we␈α
need␈α
to␈α
look␈α
through␈α
the␈α
␈↓αcontrol␈↓-list␈α∞for␈α
the
␈↓ ↓H␈↓last block designating a ␈↓αprog␈↓ entry. We ␈↓αrestore␈↓ to that saved state and set ␈↓αcontrol␈↓ to that prior point.

␈↓ ↓H␈↓αevreturn <= λ[[ ]␈↓ β(exp ← first[args];
␈↓ ↓H␈↓α␈↓ β(save_cont[ ␈↓λ`␈↓αret1; ␈↓λ`␈↓αpeval] ]

␈↓ ↓H␈↓αret1 <= λ[[ ] prog_exit[receive[]]]

␈↓ ↓H␈↓αprog_exit <= λ[[val]␈↓ βhcontrol ← find_prog[control];
␈↓ ↓H␈↓α␈↓ βhrestore␈↓λ'␈↓α[type;args;fun;dest;env;exp;cont];
␈↓ ↓H␈↓α␈↓ βhsend[val] ]

␈↓ ↓H␈↓The␈α∂␈↓αgo␈↓␈α∂statement␈α∂is␈α∂a␈α∂bit␈α∂more␈α∂complicated.␈α∂ When␈α∂a␈α∂␈↓αgo␈↓␈α∂statement␈α∂is␈α∂recognized,␈α∂we␈α⊂look␈α∂back
␈↓ ↓H␈↓through␈αthe␈αdynamic␈αchain␈αto␈αfind␈αthe␈αfirst␈αoccurrence␈αof␈αthe␈αdesired␈αlabel.␈αIf␈αwe␈αare␈αin␈αa␈α␈↓αprog␈↓␈αwe
␈↓ ↓H␈↓check␈αthe␈αcurrent␈α␈↓αgolist␈↓;␈α
if␈αthe␈αlabel␈αis␈αnot␈α
found,␈αor␈αif␈αwe␈αare␈α
not␈αimmediately␈αin␈αa␈α␈↓αprog␈↓,␈α
we␈αlook
␈↓ ↓H␈↓for␈αthe␈αlatest␈α␈↓αgolist␈↓␈α
and␈αsearch␈αit.␈αWe␈αcontinue␈α
this␈αprocess␈αuntil␈αwe␈α
discover␈αthe␈αlabel.␈αAt␈αthat␈α
time
␈↓ ↓H␈↓we␈αrestore␈αthe␈α
environment␈αto␈αthat␈α
which␈αwas␈αcurrent,␈αreset␈α
␈↓αcontrol␈↓,␈αand␈αcontinue␈α
the␈αcomputation
␈↓ ↓H␈↓at that point.

␈↓ ↓H␈↓αevgo <= λ[[ ]␈↓ αxexp ← first[args];
␈↓ ↓H␈↓α␈↓ αx[isconst[] →err[BAD_PROG_LABEL];
␈↓ ↓H␈↓α␈↓ αx not[isvar[]] →␈↓ ∧8save_cont[ ␈↓λ`␈↓αgo1; ␈↓λ`␈↓αpeval]
␈↓ ↓H␈↓α␈↓ αx ␈↓
t␈↓α →   control ← prog_go[control;exp] ]]
␈↓ ↓H␈↓␈↓↓4.19␈↓ λVAn evaluator for ␈↓αprog␈↓     167␈↓α


␈↓ ↓H␈↓αprog_go <= λ[[cntrl;exp]prog[[ ]
␈↓ ↓H␈↓α␈↓ ∧_a␈↓ ∧8[eq[type;PROG] →␈↓ ε([check_go[exp;golist[cntrl]] →␈↓ 	(restore[env];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧8␈↓ ε(␈↓ 	(cont ← ␈↓λ`␈↓αline;
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧8␈↓ ε(␈↓ 	(return[cntrl]
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧8␈↓ ε( ␈↓
t␈↓α →   cntrl ← find_go[rest[cntrl]]; go[a] ];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ∧8 ␈↓
t␈↓α →   cntrl ← find_go[cntrl]; go[a] ]]]

␈↓ ↓H␈↓αcheck_go <= λ[[lab;glist]␈↓ ∧_[null[glist] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ ∧_ eq[lab;first[first[glist]]] →   args ← rest[first[glist]];␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ ∧_ ␈↓
t␈↓α → check_go[lab;rest[glist]] ]]


␈↓ ↓H␈↓The␈α
origins␈α
of␈α
the␈αinterpreter␈α
presented␈α
here␈α
can␈α
be␈αtraced␈α
from␈α
several␈α
sources:␈α[Bla 71],␈α
[Con 73],
␈↓ ↓H␈↓[Sus 76], [Ste 76b].



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I.␈α⊂ This␈α⊂problem␈α⊂involves␈α⊂the␈α⊂␈↓αescape␈↓␈α⊂expression␈α⊂discussed␈α⊂in␈α⊂[Rey 68]␈α⊂and␈α⊂implemented␈α⊂in␈α⊂the
␈↓ ↓H␈↓University of Paris's LISP [Gre 75].  We introduce the form:
␈↓ ↓H␈↓α␈↓ ∧M escape␈↓[<function>; <form␈↓β1␈↓>; ...;<form␈↓βn␈↓>]

␈↓ ↓H␈↓with␈α∞the␈α
following␈α∞semantics:␈α
we␈α∞evaluate␈α
the␈α∞<form␈↓βi␈↓>'s␈α
from␈α∞left␈α
to␈α∞right,␈α
returning␈α∞the␈α∞value␈α
of
␈↓ ↓H␈↓<form␈↓βn␈↓>␈α⊃unless␈α⊃we␈α⊂encounter␈α⊃an␈α⊃application␈α⊂involving␈α⊃<function>.␈α⊃If␈α⊂such␈α⊃an␈α⊃application␈α⊂␈↓↓does␈↓
␈↓ ↓H␈↓appear␈αwe␈αperform␈αthat␈αapplication␈αand␈αimmediately␈αreturn␈αthe␈αresulting␈αvalue␈αas␈αthe␈αvalue␈αof␈αthe
␈↓ ↓H␈↓␈↓αescape␈↓ expression.

␈↓ ↓H␈↓Extend our latest evaluator to recognize and execute the ␈↓αescape␈↓ expression.

␈↓ ↓H␈↓II.␈α⊂The␈α⊃semantics␈α⊂of␈α⊃␈↓αgo␈↓␈α⊂specified␈α⊃that␈α⊂the␈α⊂argument␈α⊃would␈α⊂be␈α⊃evaluated␈α⊂if␈α⊃it␈α⊂were␈α⊃a␈α⊂function
␈↓ ↓H␈↓application, however the current ␈↓αpeval␈↓ does not handle this case. Correct that oversight.

␈↓ ↓H␈↓III. Extend ␈↓αevcond␈↓ to handle conditional statements.

␈↓ ↓H␈↓IV.␈α
Recall␈α
our␈α∞discussion␈α
on␈α
page 152␈α∞of␈α
the␈α
implementation␈α
of␈α∞␈↓αsave␈↓␈α
and␈α
␈↓αrestore␈↓.␈α∞Implement␈α
␈↓αsave␈↓
␈↓ ↓H␈↓and ␈↓αrestore␈↓ in ␈↓αpeval␈↓.

␈↓ ↓H␈↓V. Write ␈↓αfind_go␈↓ and ␈↓αfind_prog␈↓.

␈↓ ↓H␈↓VI. Revise ␈↓αeveval␈↓ to handle calls on ␈↓αeval␈↓ with either one or two arguments. See page 164.

␈↓ ↓H␈↓VII. MULTIPLE SETQS
␈↓ ↓H␈↓␈↓↓168  Evaluation␈↓ $4.20␈↓


␈↓ ↓H␈↓␈↓ ¬+␈↓↓4.20  Alternatives to ␈↓αeval␈↓␈↓α


␈↓ ↓H␈↓We␈αhave␈αseen␈αa␈αlot␈αof␈αevaluators␈αfor␈αLISP.␈αWe␈αshould␈αat␈αleast␈αlook␈αa␈αbit␈αat␈αother␈αpossibilities␈αfor
␈↓ ↓H␈↓describing␈α∀computational␈α∪behavior.␈α∀ Indeed,␈α∪what␈α∀is␈α∪"computation"?␈α∀ When␈α∪we␈α∀are␈α∀given␈α∪an
␈↓ ↓H␈↓expression␈α∞to␈α∞evaluate␈α∞we␈α∞are␈α∞really␈α∞simulating␈α∞the␈α∞application␈α∞of␈α∞simplification␈α∂and␈α∞substitution
␈↓ ↓H␈↓rules.␈α
 The␈αsimplification␈α
rules␈αtell␈α
us␈αwhen␈α
an␈αexpression␈α
can␈αbe␈α
replaced␈αby␈α
another␈αexpression;
␈↓ ↓H␈↓typically␈α⊂we␈α⊂think␈α⊂of␈α⊂the␈α⊂replacing␈α⊂expression␈α⊂as␈α⊂being␈α⊂"simpler"␈α⊂than␈α⊂the␈α⊂replaced␈α⊂expression.
␈↓ ↓H␈↓Thus ␈↓αcar[(A . B)]␈↓ can be replaced by ␈↓αA␈↓, or ␈↓α[␈↓
t␈↓ → 2; ...] can be replaced by ␈↓α2␈↓.

␈↓ ↓H␈↓The␈α∃substitution␈α∀rules␈α∃typically␈α∃allow␈α∀us␈α∃to␈α∀replace␈α∃a␈α∃procedure␈α∀call␈α∃with␈α∃an␈α∀appropriately
␈↓ ↓H␈↓instantiated␈α∂copy␈α∂of␈α∂the␈α∂procedure␈α∂body.␈α∂ Thus␈α∂a␈α∂computation␈α∂involving␈α∂␈↓αappend[(A B);(2 3)]␈↓␈α∞is
␈↓ ↓H␈↓identical to that obtained by replacing the occurrence of
␈↓ ↓H␈↓α␈↓ ↓fappend[(A B);(2 3)]␈↓ by␈↓α [null[(A B)] → (2 3); ␈↓
t␈↓α → concat[first[(A B)];append[rest[(A B)];(2 3)]]]

␈↓ ↓H␈↓The␈α
result␈α
of␈α
such␈α
a␈α
substitution␈αis␈α
usually␈α
a␈α
candidate␈α
for␈α
further␈α
substitutions␈αand␈α
simplifications.
␈↓ ↓H␈↓The␈αcollection␈α
of␈αsimplification␈αand␈α
substitution␈αrules␈αis␈α
called␈αthe␈αreduction␈α
rules␈αfor␈αthe␈α
language.
␈↓ ↓H␈↓Given␈α∞an␈α∞expression,␈α∂a␈α∞computation␈α∞of␈α∂its␈α∞value␈α∞is␈α∂said␈α∞to␈α∞terminate␈α∂when␈α∞there␈α∞are␈α∂no␈α∞further
␈↓ ↓H␈↓reduction rules applicable and the reduced expression is a constant of the language.

␈↓ ↓H␈↓The␈α∞difficulties␈α∞with␈α∞these␈α
schemes␈α∞come␈α∞from␈α∞both␈α
practical␈α∞and␈α∞theoretical␈α∞considerations.␈α
The
␈↓ ↓H␈↓direct␈α
application␈α
of␈α
reduction␈α
rules␈α
is␈αquite␈α
inefficient:␈α
making␈α
textual␈α
substitutions␈α
is␈αexpensive.
␈↓ ↓H␈↓Instead␈α∞we␈α∞developed␈α∞the␈α∞ideas␈α∞of␈α∞symbol␈α
tables␈α∞to␈α∞contain␈α∞the␈α∞bindings␈α∞of␈α∞the␈α∞variables,␈α
rather
␈↓ ↓H␈↓than perform the actual substitutions.

␈↓ ↓H␈↓The␈αtheoretical␈αdifficulty␈αappears␈αsince,␈αat␈αany␈αtime␈αin␈αa␈αcomputation,␈αthere␈αmay␈αbe␈αmore␈αthan␈αone
␈↓ ↓H␈↓reduction␈α⊂rule␈α∂which␈α⊂is␈α∂applicable.␈α⊂A␈α∂further␈α⊂difficulty␈α∂is␈α⊂that␈α∂one␈α⊂sequence␈α∂of␈α⊂reductions␈α∂may
␈↓ ↓H␈↓terminate,␈α
while␈α
another␈αsequence␈α
of␈α
reductions␈α
is␈αnon-terminating.␈α
We␈α
have␈α
seen␈αthis␈α
phenomenon
␈↓ ↓H␈↓in previous discussions of call-by-value versus call-by-name, and inner-most versus outer-most.

␈↓ ↓H␈↓LISP␈α⊗opted␈α⊗for␈α⊗the␈α∃call-by-value␈α⊗interpretation␈α⊗of␈α⊗expressions.␈α∃It␈α⊗is␈α⊗possible␈α⊗to␈α⊗develop␈α∃a
␈↓ ↓H␈↓call-by-name␈αevaluator.␈α
Call-by-name␈αimplies␈α
that␈αwe␈αsubstitute␈α
the␈αunevaluated␈α
actual␈αparameters
␈↓ ↓H␈↓for␈αthe␈αformal␈αparameters.␈α As␈αin␈α␈↓αeval␈↓,␈αwe␈αneed␈αnot␈αmake␈αexplicit␈αsubstitutions;␈αappropriate␈αuse␈αof
␈↓ ↓H␈↓symbol␈α⊂tables␈α⊂will␈α⊂simulate␈α⊂the␈α∂action.␈α⊂ But␈α⊂now,␈α⊂when␈α⊂we␈α⊂build␈α∂a␈α⊂symbol␈α⊂table␈α⊂on␈α⊂entry␈α⊂to␈α∂a
␈↓ ↓H␈↓λ-expression␈α
we␈α
bind␈α
the␈αactual␈α
expressions␈α
to␈α
the␈α
λ-variables;␈αwhen␈α
we␈α
encounter␈α
a␈α
variable␈αin␈α
the
␈↓ ↓H␈↓body␈αof␈α
the␈αexpression␈αwe␈α
evaluate␈αthe␈α
actual␈αparameter.␈αThe␈α
difficulty␈αis␈α
that␈αan␈αactual␈α
parameter
␈↓ ↓H␈↓itself␈α
may␈α
contain␈α
variables,␈α
and␈α
those␈αvariables␈α
need␈α
to␈α
be␈α
interpreted␈α
in␈α
the␈αbinding␈α
environment.
␈↓ ↓H␈↓This means that we must bind ␈↓αfunarg␈↓-like expressions to the formal parameters.

␈↓ ↓H␈↓Most␈α∂of␈α⊂␈↓αeval␈↓βname␈↓␈α∂is␈α∂like␈α⊂␈↓αeval␈↓␈α∂of␈α⊂Section 4.5,␈α∂so␈α∂we␈α⊂only␈α∂sketch␈α∂the␈α⊂interesting␈α∂parts.␈α⊂Assume␈α∂the
␈↓ ↓H␈↓␈↓αfunarg␈↓-expression␈α∨we␈α manufacture␈α∨has␈α∨two␈α components,␈α∨the␈α∨␈↓αexpr␈↓-component,␈α and␈α∨the
␈↓ ↓H␈↓␈↓αenv␈↓-component.

␈↓ ↓H␈↓We␈α⊃can␈α∩implement␈α⊃␈↓↓eval␈↓βname␈↓␈α∩by␈α⊃simply␈α⊃changing␈α∩the␈α⊃symbol␈α∩table␈α⊃orgainzation,␈α∩supplying␈α⊃new
␈↓ ↓H␈↓versions of ␈↓αlookup␈↓ and ␈↓αmkenv␈↓. See page 106 and page 133.
␈↓ ↓H␈↓␈↓↓4.20␈↓ λxAlternatives to ␈↓αeval␈↓     169␈↓α



␈↓ ↓H␈↓α␈↓ ¬Salloc <= λ[[vars] ()]

␈↓ ↓H␈↓α␈↓ ∧	send <= λ[[var;val;dest] concat[mkent[var;val];dest]

␈↓ ↓H␈↓α␈↓ ∧elink <= λ[[dest;env] concat[dest;env]]

␈↓ ↓H␈↓αlookup <= λ[[var;env] l␈↓λ'␈↓α[var;first[env];rest[env]]

␈↓ ↓H␈↓αl␈↓λ'␈↓α <= λ[[n;bl;env]␈↓ β8[null[bl] → l␈↓λ'␈↓α[n;first[env];rest[env]]
␈↓ ↓H␈↓α␈↓ β8 eq[n;name[first[bl]] → eval[value[first[bl]];env]
␈↓ ↓H␈↓α␈↓ β8 ␈↓
t␈↓α → l␈↓λ'␈↓α[n;rest[bl];env] ]]

␈↓ ↓H␈↓One␈αadvantage␈αof␈αsuch␈αan␈αevaluator␈αis␈αthat␈αit␈αwill␈αnot␈αevaluate␈αa␈αparameter␈αuntil␈αit␈αactually␈αneeds
␈↓ ↓H␈↓it,␈αwhereas␈α␈↓αeval␈↓␈αevaluates␈αall␈αparameters␈αat␈αfunction␈αentry␈αtime.␈αIf␈αan␈αactual␈αparameter␈αis␈α
not␈αused
␈↓ ↓H␈↓in␈α∂the␈α∂computation␈α∂and␈α∂the␈α∂computation␈α∂of␈α∂that␈α∂parameter␈α∂fails␈α∂to␈α∂terminate,␈α∂then␈α⊂␈↓αeval␈↓βname␈↓␈α∂will
␈↓ ↓H␈↓terminate␈αwhile␈α␈↓αeval␈↓␈αwill␈αnot.␈α There␈αare␈αdisadvantages␈αto␈α␈↓αeval␈↓βname␈↓.␈αEvery␈αoccurrence␈αof␈αa␈αvariable
␈↓ ↓H␈↓within␈αthe␈αbody␈αof␈α
the␈αfunction␈αwill␈αinvolve␈αa␈α
re-evaluation␈αof␈αthe␈αcorresponding␈αactual␈α
parameter.
␈↓ ↓H␈↓If␈αthere␈αare␈αno␈αside-effects␈αin␈αthe␈αcomputation␈αthen␈αthese␈αfurther␈αcomputations␈αare␈αan␈αunnecessary
␈↓ ↓H␈↓expense.␈α_Several␈α_people␈α_([Wad 71],␈α_[Vui 73],␈α_[Pac 73],␈α_[Hen 76],␈α_[Fri 75a])␈α_have␈α↔suggested
␈↓ ↓H␈↓modifications␈α∩to␈α∩␈↓αeval␈↓βname␈↓␈α∩to␈α⊃reduce␈α∩the␈α∩inefficiency.␈α∩ The␈α⊃basic␈α∩idea,␈α∩named␈α∩call-by-need␈α∩is␈α⊃to
␈↓ ↓H␈↓proceed␈α
in␈α
the␈α
␈↓αeval␈↓βname␈↓␈α
style␈α∞until␈α
the␈α
first␈α
use␈α
of␈α
a␈α∞variable.␈α
At␈α
that␈α
time␈α
we␈α
evaluate␈α∞the␈α
actual
␈↓ ↓H␈↓parameter,␈α∂and␈α∂modify␈α∞the␈α∂symbol␈α∂table,␈α∞␈↓↓replacing␈↓␈α∂the␈α∂actual␈α∞parameter␈α∂with␈α∂its␈α∂value.␈α∞Further
␈↓ ↓H␈↓references␈α∞to␈α∞that␈α∞variable␈α∞simply␈α∞get␈α∞the␈α∞value.␈α∞Obviously␈α∞the␈α∞scheme␈α∞will␈α∞not␈α∞work␈α∞correctly␈α
if
␈↓ ↓H␈↓side-effects are present.  We leave it to the reader to supply the details of ␈↓αeval␈↓βneed␈↓. See page 171.

␈↓ ↓H␈↓We␈αnow␈αexplore␈αa␈αdifferent␈αkind␈αof␈αmodification␈αto␈αLISP.␈αThis␈αone␈αis␈αgrounded␈αmore␈αin␈αpractical
␈↓ ↓H␈↓experience␈α
with␈αthe␈α
programming␈αlanguage,␈α
though␈αthe␈α
results␈αdo␈α
have␈αtheoretical␈α
interest.␈α
It␈αhas
␈↓ ↓H␈↓been␈α∂noted␈α∂that␈α∞programmers␈α∂frequently␈α∂wish␈α∞to␈α∂return␈α∂more␈α∞than␈α∂one␈α∂value␈α∞as␈α∂the␈α∂result␈α∂of␈α∞a
␈↓ ↓H␈↓function␈α∂application.␈α∂The␈α∂standard␈α∂alternatives␈α∂are␈α∞to␈α∂make␈α∂global␈α∂assignments␈α∂from␈α∂within␈α∞the
␈↓ ↓H␈↓body␈α
of␈α∞the␈α
function,␈α
or␈α∞to␈α
return␈α∞a␈α
list␈α
of␈α∞the␈α
desired␈α∞values␈α
making␈α
it␈α∞the␈α
responsibility␈α∞of␈α
the
␈↓ ↓H␈↓calling␈α↔program␈α⊗to␈α↔select␈α↔the␈α⊗proper␈α↔components.␈α↔ Neither␈α⊗alternative␈α↔is␈α↔particularly␈α⊗good.
␈↓ ↓H␈↓Programming␈α⊃with␈α⊃side-effects␈α⊃tends␈α⊃to␈α⊃lead␈α⊃to␈α⊃obscure␈α⊃programs;␈α⊃passing␈α⊃lists␈α⊃back␈α⊃as␈α⊂values
␈↓ ↓H␈↓requires␈α
much␈αadditional␈α
computation:␈α
someone␈αmust␈α
build␈α
the␈αlist;␈α
someone␈α
must␈αtear␈α
it␈α
apart.␈αIt␈α
is
␈↓ ↓H␈↓also␈α∩disturbing␈α⊃that␈α∩the␈α⊃operation␈α∩being␈α∩modelled,␈α⊃--multiple-values--,␈α∩is␈α⊃not␈α∩recognizable␈α∩as␈α⊃a
␈↓ ↓H␈↓construct.␈α⊂ This␈α⊂is␈α⊂a␈α⊂similar␈α⊂complaint␈α⊂to␈α⊂that␈α⊂we␈α⊂raised␈α⊂in␈α⊂discussing␈α⊂labels-and-␈↓αgo␈↓s␈α⊂versus␈α⊂an
␈↓ ↓H␈↓iterative construct.

␈↓ ↓H␈↓Our␈α⊃goal␈α⊃is␈α⊃realizable␈α⊃by␈α⊃a␈α⊃slight␈α⊃extension␈α⊃of␈α⊃the␈α⊃extended␈α⊃conditionals␈α⊃and␈α⊃multiple-bodied
␈↓ ↓H␈↓λ-expressions (page 143, page 146).  We will interpret the form:
␈↓ ↓H␈↓␈↓ ¬{p␈↓βi␈↓ → e␈↓βi1␈↓; ... e␈↓βin␈↓.

␈↓ ↓H␈↓as␈α
returning␈αthe␈α
e␈↓βij␈↓-values␈α
to␈αthe␈α
calling␈α
function␈αin␈α
a␈αleft-to-right␈α
order.␈α
If␈αthe␈α
calling␈α
program␈αis
␈↓ ↓H␈↓single-valued␈α∩then␈α∩the␈α∩value␈α∩it␈α∩sees␈α∩is␈α∪the␈α∩value␈α∩of␈α∩e␈↓βin␈↓.␈α∩This␈α∩is␈α∩compatible␈α∩with␈α∪our␈α∩current
␈↓ ↓H␈↓interpretation.  The evaluation of:
␈↓ ↓H␈↓␈↓ ¬3␈↓αλ[[ ... ]f␈↓β1␈↓α[ ... ]; ...; f␈↓βn␈↓α[ ... ]]␈↓
␈↓ ↓H␈↓␈↓↓170  Evaluation␈↓ $4.20␈↓


␈↓ ↓H␈↓will be interpreted similarly.

␈↓ ↓H␈↓For␈α∂example␈α∂[Fri 75]␈α∂discusses␈α∂a␈α∂multiple-valued␈α∂function␈α∂named␈α∂␈↓αsigmasum␈↓.␈α∂This␈α∂function␈α⊂is␈α∂to
␈↓ ↓H␈↓take␈αa␈αlist␈αof␈αnumbers␈αand␈αreturn␈αthree␈αitems:␈αthe␈αlength␈αof␈αthe␈αlist,␈αthe␈αsum␈αof␈αthe␈αnumbers␈αin␈αthe
␈↓ ↓H␈↓list,␈α∂and␈α⊂the␈α∂sum␈α∂of␈α⊂the␈α∂squares␈α⊂of␈α∂the␈α∂numbers␈α⊂in␈α∂the␈α⊂list.␈α∂ In␈α∂our␈α⊂notation␈α∂␈↓αsigmasum␈↓␈α⊂can␈α∂be
␈↓ ↓H␈↓expressed as:

␈↓ ↓H␈↓αsigmasum <= λ[[x]␈↓ βX[null[x] → 0;0;0;
␈↓ ↓H␈↓α␈↓ βX ␈↓
t␈↓α → λ[␈↓ ∧8[z␈↓β1␈↓α;z␈↓β2␈↓α;z␈↓β3␈↓α]␈↓ ¬8add1[z␈↓β1␈↓α];
␈↓ ↓H␈↓α␈↓ βX␈↓ ∧8␈↓ ¬8plus[first[x];z␈↓β2␈↓α];
␈↓ ↓H␈↓α␈↓ βX␈↓ ∧8␈↓ ¬8plus[times[first[x];first[x]];z␈↓β3␈↓α] ]
␈↓ ↓H␈↓α␈↓ βX␈↓ ∧8[sigmasum[rest[x]]] ]]


␈↓ ↓H␈↓Notice␈α∞that␈α∞we␈α∞use␈α∞an␈α∂anonymous␈α∞λ-expression␈α∞to␈α∞spread␈α∞the␈α∂multiple␈α∞values␈α∞at␈α∞the␈α∞level␈α∂of␈α∞the
␈↓ ↓H␈↓caller.

␈↓ ↓H␈↓Another␈α
example␈α
is␈α
a␈α
solution␈α
to␈α∞the␈α
␈↓αsamefringe␈↓␈α
problem:␈α
determine␈α
whether␈α
or␈α
not␈α∞the␈α
terminal
␈↓ ↓H␈↓nodes of two trees are the same, respecting order, but irrespective of tree structure. Thus:

␈↓ ↓H␈↓α␈↓ α←samefringe[(A (B (C))); (A B C)] = ␈↓
t␈↓ but ␈↓αsamefringe[(A (B C)); (A C B)] = ␈↓
f␈↓.

␈↓ ↓H␈↓αsamefringe <= λ[[x;y]␈↓ βx[null[x] → null[y];
␈↓ ↓H␈↓α␈↓ βx ␈↓
t␈↓α →␈↓ ∧Hλ[[z␈↓β1␈↓α;z␈↓β2␈↓α;z␈↓β3␈↓α;z␈↓β4␈↓α][eq[z␈↓β1␈↓α;z␈↓β3␈↓α] → samefringe[z␈↓β2␈↓α;z␈↓β4␈↓α]; ␈↓
t␈↓α → ␈↓
f␈↓α]]
␈↓ ↓H␈↓α␈↓ βx␈↓ ∧H   [fringe[x];fringe[y]] ]

␈↓ ↓H␈↓αfringe <= λ[[x]␈↓ β8[atom[first[x]] → first[x]; rest[x];
␈↓ ↓H␈↓α␈↓ β8 ␈↓
t␈↓α →␈↓ ∧_λ[[y;z] y;␈↓ ¬_[null[z] → rest[x];
␈↓ ↓H␈↓α␈↓ β8␈↓ ∧_␈↓ ¬_ ␈↓
t␈↓α → cons[z; rest[x]] ] ]
␈↓ ↓H␈↓α␈↓ β8␈↓ ∧_    [fringe[first[x]]] ] ]

␈↓ ↓H␈↓In␈α
this␈α
solution,␈α␈↓αsamefringe␈↓␈α
is␈α
single-valued␈αbut␈α
uses␈α
values␈αfrom␈α
a␈α
multiple-valued␈α
function.␈αThe
␈↓ ↓H␈↓two␈αvalues␈αfrom␈α␈↓αfringe[x]␈↓␈αare␈αspread␈αinto␈α␈↓αz␈↓β1␈↓␈αand␈α␈↓αz␈↓β2␈↓␈αand␈αthe␈αvalues␈αfrom␈α␈↓αfringe[y]␈↓␈αare␈αspread␈αinto
␈↓ ↓H␈↓␈↓αz␈↓β3␈↓ and ␈↓αz␈↓β4␈↓.

␈↓ ↓H␈↓It␈αis␈αeasy␈αto␈αwrite␈αan␈αevaluator␈αfor␈αsuch␈αmultiple-valued␈αexpressions.␈α Here␈αis␈αa␈αsketch␈αof␈αthe␈αbasic
␈↓ ↓H␈↓parts:

␈↓ ↓H␈↓αmeval <= λ[[x;e]␈↓ β([isconst[x] → list[denote[x]];
␈↓ ↓H␈↓α␈↓ β( isvar[x] →list[lookup[x;e]];
␈↓ ↓H␈↓α␈↓ β( iscond[x] → mevcond[condbody[x];e];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → mapply[fun[x];mevlist[arglist[x];e];e] ]]
␈↓ ↓H␈↓␈↓↓4.20␈↓ λxAlternatives to ␈↓αeval␈↓     171␈↓α



␈↓ ↓H␈↓αmapply <= λ[[fn;args;e]␈↓ ∧_[isprim[fun] →list[apply[fun;args;e]]
␈↓ ↓H␈↓α␈↓ ∧_ islambda[fun] → mevlist[␈↓ εXbodylist[fun];
␈↓ ↓H␈↓α␈↓ ∧_␈↓ ¬X␈↓ εXmkenv[vars[fun];args;e] ]
␈↓ ↓H␈↓α␈↓ ∧_ ␈↓
t␈↓α → mapply[eval[fun;e];args;e] ]]

␈↓ ↓H␈↓αmevlist <= λ[[l;e]␈↓ β([null[l] → ();
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → append[meval[first[l];e]; mevlist[rest[l];e]] ]]

␈↓ ↓H␈↓αmevcond <= λ[[l;e]␈↓ βH[null[l] → ();
␈↓ ↓H␈↓α␈↓ βH first[meval[pred[first[l]];e]] → mevlist[conseq[first[l];env];
␈↓ ↓H␈↓α␈↓ βH ␈↓
t␈↓α → mevcond[rest[l];e] ]]



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Complete the specification of ␈↓αeval␈↓βname␈↓.

␈↓ ↓H␈↓II.␈α
Complete␈α
the␈α∞specification␈α
of␈α
␈↓αeval␈↓βneed␈↓.␈α∞To␈α
do␈α
this,␈α∞you␈α
many␈α
assume␈α∞the␈α
existence␈α
of␈α∞a␈α
binary
␈↓ ↓H␈↓function␈αnamed␈α␈↓αstuff␈↓␈αwhose␈αfirst␈αargument␈α␈↓αx␈↓␈αis␈αthe␈αa␈αname␈αin␈αthe␈αsymbol␈αtable,␈αand␈αwhose␈αsecond
␈↓ ↓H␈↓argument ␈↓αy␈↓ is a value.  ␈↓αstuff␈↓ is to replace the current binding of ␈↓αx␈↓ with ␈↓αy␈↓.

␈↓ ↓H␈↓III. Modify ␈↓αpeval␈↓ to handle multiple-valued functions.

␈↓ ↓H␈↓IV.␈α Recall␈αthe␈αproblem␈αon␈αpage 134␈αdealing␈αwith␈αthe␈αanalysis␈αof␈α␈↓αlookup␈↓.␈αInclude␈αcall-by-name␈αand
␈↓ ↓H␈↓call-by-need in your analysis.

␈↓ ↓H␈↓V.␈α∞Using␈α∞the␈α∞results␈α∞of␈α
the␈α∞previous␈α∞problem,␈α∞make␈α∞up␈α∞a␈α
table␈α∞whose␈α∞rows␈α∞are␈α∞labeled␈α∞with␈α
the
␈↓ ↓H␈↓binding␈αimplementations:␈α"deep"␈α"shallow",␈α"Weizenbaum",␈α"need",␈αand␈α"name";␈αand␈αwhose␈αcolumns
␈↓ ↓H␈↓are labeled with the prmitives: ␈↓αalloc␈↓, ␈↓αlink␈↓, ␈↓αsend␈↓, and the primitives for ␈↓αlookup␈↓. Supply the entries.



␈↓ ↓H␈↓␈↓ ¬"␈↓↓4.21  Function definitions␈↓


␈↓ ↓H␈↓Now␈αthat␈αwe␈αhave␈αdeveloped␈αthese␈αmore␈α
explicit␈αevaluators,␈αwe␈αshould␈αbe␈αable␈αto␈αexploit␈α
some␈αof
␈↓ ↓H␈↓this␈α
additional␈α∞detail.␈α
In␈α
particular,␈α∞more␈α
of␈α∞the␈α
detail␈α
of␈α∞"<="␈α
should␈α
be␈α∞apparent.␈α
The␈α∞effect␈α
of
␈↓ ↓H␈↓␈↓αf <= λ[[x]␈↓λx␈↓α]␈↓␈α∞is␈α∂to␈α∞put␈α∂the␈α∞definition␈α∂of␈α∞␈↓αf␈↓␈α∞in␈α∂the␈α∞global␈α∂environment,␈α∞whereas␈α∂␈↓αlabel␈↓␈α∞creates␈α∂a␈α∞new
␈↓ ↓H␈↓dest-block␈α∞with␈α∞␈↓αf␈↓␈α∞bound␈α∞to␈α∞a␈α∞␈↓αfunarg␈↓␈α∞consisting␈α∞of␈α∞␈↓λx␈↓␈α∞and␈α∞that␈α∞constructed␈α∞environment.␈α∞Once␈α
we
␈↓ ↓H␈↓leave␈αthe␈αenvironment␈αcontaining␈αthe␈α␈↓αlabel␈↓␈αdefinition,␈αthat␈αdefinition␈αis␈αeffectively␈α
destroyed.␈α The
␈↓ ↓H␈↓effect␈αof␈α"<="␈αis␈αto␈αbe␈αglobal;␈αwe␈αcould␈αimplement␈α␈↓αf␈↓ <= ␈↓λx␈↓␈αas␈α␈↓αset[f;␈↓λx␈↓α]␈↓␈αexcept␈αthat␈αthe␈αdefinition␈αmust
␈↓ ↓H␈↓always␈α
go␈α
in␈α
the␈α
global␈α
table.␈α
 Note␈α∞that␈α
a␈α
"<="-definition␈α
could␈α
be␈α
temporarily␈α
superseeded␈α∞by␈α
a
␈↓ ↓H␈↓␈↓↓172  Evaluation␈↓ &4.21␈↓


␈↓ ↓H␈↓␈↓αlabel␈↓-definition␈α
to␈αthe␈α
same␈α
name␈αand␈α
therefore␈αour␈α
search␈α
for␈αa␈α
binding␈α
for␈α␈↓αf␈↓␈α
may␈αnot␈α
short-circuit
␈↓ ↓H␈↓the environment chain.

␈↓ ↓H␈↓Our␈αsearch␈αstrategy␈αis␈αencoded␈αin␈αthe␈α␈↓αlookup␈↓␈αfunction;␈αusing␈αthe␈αcurrent␈αenvironment,␈αwe␈αfind␈αthe
␈↓ ↓H␈↓latest␈αbinding␈αfor␈α
a␈αvariable.␈α With␈αthe␈α
␈↓αprog␈↓␈αevaluator,␈α␈↓αpeval␈↓β1␈↓␈αof␈α
page 161,␈αthings␈αhave␈α
become␈αa
␈↓ ↓H␈↓bit␈α
more␈αcomplicated.␈α
Besides␈α
finding␈αthe␈α
definition␈αwe␈α
must␈α
also␈αdetermine␈α
whether␈αthe␈α
arguments
␈↓ ↓H␈↓are␈αto␈αbe␈αevaluated.␈α The␈αdevice␈αof␈α␈↓αisspec␈↓␈α(page 160)␈αis␈αsufficient␈αfor␈αthe␈αevaluators,␈αbut␈αhas␈αsome
␈↓ ↓H␈↓difficulties␈αif␈αwe␈αwish␈αto␈αallow␈αuser-defined␈αspecial␈αforms.␈αWe␈αwill␈αdevelop␈αa␈αsyntax␈αfor␈αexpressing
␈↓ ↓H␈↓special forms at the user level, and then discuss problems of implementation.

␈↓ ↓H␈↓We␈αwill␈αdefine␈α"<␈↓βf␈↓="␈αto␈αmean␈α"..is␈αdefined␈αto␈αbe␈αa␈αspecial␈αform...".␈α A␈αspecial-form␈αdefinition␈αis␈αalso
␈↓ ↓H␈↓called a ␈↓↓fexpr␈↓; and a call-by-value definition is called an ␈↓↓expr␈↓.

␈↓ ↓H␈↓An␈α
fexpr␈α
is␈αdefined␈α
with␈α
either␈αone␈α
or␈α
two␈αformal␈α
parameters.␈α
The␈αfirst␈α
parameter␈α
is␈αalways␈α
bound
␈↓ ↓H␈↓to␈αthe␈αlist␈αof␈α␈↓↓unevaluated␈↓␈αactual␈αparameters.␈α If␈αthe␈αdefinition␈αhas␈αa␈αsecond␈αformal␈αparameter,␈αthen
␈↓ ↓H␈↓the␈αenvironment␈αat␈αthe␈αpoint␈αof␈αcall␈αis␈αassigned␈αto␈αthe␈αsecond␈αparameter.␈α This␈αdistinction␈αneeds␈αto
␈↓ ↓H␈↓be␈α
made␈α
when␈α
we␈α
expect␈α
to␈α
perform␈α
some␈α
evaluation␈α
of␈α
the␈α
formal␈α
parameters␈α
within␈α∞the␈α
fexpr.
␈↓ ↓H␈↓Using␈αthe␈αimplementation␈αof␈α␈↓αeval␈↓␈αdiscussed␈αon␈αpage 164␈αwe␈αcan␈αwrite␈αeither␈α␈↓αeval[␈↓<form>;<env>]␈αor
␈↓ ↓H␈↓␈↓αeval␈↓[<form>].␈αIn␈αthe␈αlatter␈αcase␈αthe␈αenvironment␈αthat␈αis␈αused␈αis␈αthe␈αenvironment␈αwhich␈αwas␈αcurrent
␈↓ ↓H␈↓when the ␈↓αeval␈↓ is performed. Sometimes this is not the desired environment.  Consider:

␈↓ ↓H␈↓α␈↓ βxf1 <␈↓βf␈↓α= λ[[x] prog[[y]
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬(y ← 2;
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬(return[eval[first[x]]]]

␈↓ ↓H␈↓If␈α
we␈α
execute␈α
␈↓αf1[0]␈↓,␈α
␈↓αx␈↓␈α
will␈α
be␈α
bound␈α
to␈αthe␈α
list␈α
␈↓α(0)␈↓␈α
and␈α
␈↓αeval[first[x]]␈↓␈α
will␈α
return␈α
␈↓α0␈↓␈α
as␈α
expected.␈αBut␈α
if
␈↓ ↓H␈↓we execute:
␈↓ ↓H␈↓α␈↓ βxy ← 0;
␈↓ ↓H␈↓α␈↓ βxf1[y];

␈↓ ↓H␈↓then␈α␈↓αx␈↓␈αgets␈αbound␈αto␈α␈↓α(Y)␈↓,␈αand␈α␈↓αeval[Y]␈↓␈αwill␈αfind␈αthe␈αvalue␈αassociated␈αwith␈α␈↓αY␈↓␈αto␈αbe␈α␈↓α2␈↓,␈αand␈αthe␈αvalue
␈↓ ↓H␈↓of ␈↓αf1[y]␈↓ is ␈↓α2␈↓, rather than the expected ␈↓α0␈↓.

␈↓ ↓H␈↓Clearly,␈αthe␈αproblem␈αis␈αthat␈αthe␈αcall␈αon␈α␈↓αeval␈↓␈αtakes␈αplace␈αin␈αthe␈αwrong␈αenvironment.␈α We␈αcan␈αcorrect
␈↓ ↓H␈↓this␈α
by␈α
making␈α
the␈α
definition␈α
with␈α
␈↓↓two␈↓␈α
arguments,␈α
binding␈α
the␈α
second␈α
to␈α
the␈α
environment␈α
at␈α
the
␈↓ ↓H␈↓point of call to the fexpr:
␈↓ ↓H␈↓α␈↓ βxf2 <␈↓βf␈↓α= λ[[x;a] prog[[y]
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬Hy ← 2;
␈↓ ↓H␈↓α␈↓ βx␈↓ ¬Hreturn[eval[first[x];a]]]

␈↓ ↓H␈↓α␈↓ βxy ← 0;
␈↓ ↓H␈↓α␈↓ βxf2[y];

␈↓ ↓H␈↓The call on ␈↓αf2␈↓ will use the environment with ␈↓αy␈↓ being ␈↓α0␈↓ rather than ␈↓α2␈↓.
␈↓ ↓H␈↓␈↓↓4.21␈↓ λcFunction definitions     173␈↓


␈↓ ↓H␈↓Special␈αform␈αdefinitions␈αare␈αuseful␈αin␈αseveral␈αcontexts.␈αRecall␈αthat␈αwe␈αrestricted␈αLISP␈αcall-by-value
␈↓ ↓H␈↓functions␈α∂to␈α∂have␈α∂a␈α∂fixed␈α∂number␈α∂arguments.␈α∂For␈α∂example␈α∂if␈α∂we␈α∂wish␈α∂to␈α∂add␈α∂four␈α∂numbers␈α∞or
␈↓ ↓H␈↓append three lists we have to write something like:

␈↓ ↓H␈↓α␈↓ βhplus[x␈↓β1␈↓α;[plus[x␈↓β2␈↓α;plus[x␈↓β3␈↓α;x␈↓β4␈↓α]␈↓ or ␈↓αappend[append[l␈↓β1␈↓α;l␈↓β2␈↓α];l␈↓β3␈↓α]

␈↓ ↓H␈↓Since ␈↓αplus␈↓ and ␈↓αappend␈↓ are associative operations we would rather write:

␈↓ ↓H␈↓α␈↓ ∧iplus[x␈↓β1␈↓α;x␈↓β2␈↓α;x␈↓β3␈↓α;x␈↓β4␈↓α]␈↓ or ␈↓αappend[l␈↓β1␈↓α;l␈↓β2␈↓α;l␈↓β3␈↓α]

␈↓ ↓H␈↓Using␈αa␈αspecial␈αform,␈αwe␈αcan␈αallow␈αfunctions␈α
of␈αan␈αarbitrary␈αnumber␈αof␈αarguments:␈αWe␈αcould␈α
write
␈↓ ↓H␈↓␈↓αplus␈↓ as:

␈↓ ↓H␈↓αplus <␈↓βf␈↓α= λ[␈↓ αX[l;e] prog[[sum]
␈↓ ↓H␈↓α␈↓ αX␈↓ αxsum ← 0;
␈↓ ↓H␈↓α␈↓ αXa␈↓ αx[null[l] → return[sum]];
␈↓ ↓H␈↓α␈↓ αX␈↓ αxsum ← *plus[sum;eval[first[l];e]];
␈↓ ↓H␈↓α␈↓ αX␈↓ αxl ← rest[l];
␈↓ ↓H␈↓α␈↓ αX␈↓ αxgo[a]]]


␈↓ ↓H␈↓Notice␈αthat␈αwe␈αcould␈αhave␈αused␈α␈↓αeval␈↓␈αwith␈αone␈αargument␈αunless␈αthe␈αvariables␈α␈↓αl␈↓␈αor␈α␈↓αsum␈↓␈αappeared␈αas
␈↓ ↓H␈↓constitutents of the actual parameters.

␈↓ ↓H␈↓Recalling␈αSection 4.12,␈αwe␈αcan␈αuse␈α<␈↓βf␈↓=␈αto␈αextend␈αthe␈αevaluator.␈α For␈αexample,␈α␈↓αand␈↓␈αcould␈αbe␈αdefined
␈↓ ↓H␈↓as:

␈↓ ↓H␈↓␈↓ β?␈↓αand <␈↓βf␈↓α= λ[[l;e]evand[l;e]] ␈↓where ␈↓αevand␈↓ is defined on page 135.

␈↓ ↓H␈↓The␈α∞implementation␈α∞of␈α∞␈↓αg <␈↓βf␈↓= ␈↓αλ[[x]␈↓λx␈↓]␈α∞requires␈α∞that␈α∞we␈α∂represent␈α∞the␈α∞fact␈α∞that␈α∞␈↓αg␈↓␈α∞is␈α∞a␈α∂fexpr␈α∞rather
␈↓ ↓H␈↓than␈αan␈αexpr.␈αThe␈αimplication␈αof␈α␈↓αisspec␈↓␈αof␈αpage 160␈αis␈αthat␈αwe␈αhave␈αtwo␈αtables:␈αone␈αfor␈αexprs,␈αone
␈↓ ↓H␈↓for␈α∂fexprs.␈α∂This␈α∂complicates␈α∂the␈α∂search␈α∞strategy␈α∂unnecessarily.␈α∂ Indeed␈α∂there␈α∂should␈α∂only␈α∂be␈α∞one
␈↓ ↓H␈↓definition␈α⊂or␈α⊃value␈α⊂associated␈α⊂with␈α⊃a␈α⊂name␈α⊂at␈α⊃any␈α⊂one␈α⊂time,␈α⊃so␈α⊂a␈α⊂single␈α⊃table␈α⊂should␈α⊃be␈α⊂both
␈↓ ↓H␈↓necessary␈α
and␈α∞sufficient.␈α
We␈α∞␈↓↓do␈↓␈α
need␈α∞some␈α
way␈α
of␈α∞determining␈α
the␈α∞calling␈α
style␈α∞to␈α
be␈α∞used␈α
when
␈↓ ↓H␈↓applying␈α
the␈α
definition.␈α
 One␈α
way␈α
is␈α
to␈α∞slightly␈α
revise␈α
the␈α
␈↓αisspec␈↓␈α
technique:␈α
we␈α
use␈α
␈↓αlookup␈↓␈α∞for␈α
␈↓↓all␈↓
␈↓ ↓H␈↓searches,␈αbut␈αalso␈αhave␈αa␈α
table␈αrelating␈αfunction-name␈αwith␈αits␈α
calling␈αstyle.␈αOne␈αdifficulty␈αwith␈α
this
␈↓ ↓H␈↓scheme␈αis␈αthat␈αwe␈αcould␈αnot␈αhandle␈α
anonymous␈αfexpr␈αdefinitions.␈αTherefore␈αsome␈αversions␈αof␈α
LISP
␈↓ ↓H␈↓replace␈α∪the␈α∪character␈α∪"λ"␈α∪with␈α∪another␈α∪special␈α∪character␈α∪when␈α∪making␈α∪fexpr␈α∪definitions.␈α∪For
␈↓ ↓H␈↓example:
␈↓ ↓H␈↓α␈↓ ∧⎇g <␈↓βf␈↓α= λ[[x;y]␈↓λx␈↓α] ␈↓≡␈↓α  g <= β[[x;y]␈↓λx␈↓α].

␈↓ ↓H␈↓We␈α∂would␈α∂translate␈α∂such␈α∂β-expressions␈α∂into␈α∂S-expr␈α∂form,␈α∂and␈α∂extend␈α∂the␈α∂evaluator␈α∂to␈α∞recognize
␈↓ ↓H␈↓such constructs.
␈↓ ↓H␈↓␈↓↓174  Evaluation␈↓ &4.21␈↓


␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I. Define ␈↓αlist␈↓ as a special form.

␈↓ ↓H␈↓II. Write a version of ␈↓αpeval␈↓ to handle β-expressions.

␈↓ ↓H␈↓III.␈αDefine␈αtwo␈αspecial␈αforms,␈α␈↓αde␈↓␈αand␈α␈↓αdf␈↓,␈αwhich␈αwill␈αimplement␈α<=␈αand␈α<␈↓βf␈↓=␈αrespectively.␈αThe␈αformat
␈↓ ↓H␈↓of these special forms is identical. For example:
␈↓ ↓H␈↓␈↓ ∧J␈↓αde␈↓[<name>;<formal parameters>;<body>]

␈↓ ↓H␈↓will implement
␈↓ ↓H␈↓␈↓ ∧7<name> <= λ[<formal parameters> <body>]



␈↓ ↓H␈↓␈↓ ∧b␈↓↓4.22  Rapprochement: In retrospect␈↓


␈↓ ↓H␈↓As␈α∞we␈α∞have␈α∞just␈α∞seen␈α∞there␈α∞are␈α∂alternatives␈α∞to␈α∞some␈α∞of␈α∞the␈α∞LISP-techniques␈α∞and␈α∞there␈α∂are␈α∞some
␈↓ ↓H␈↓things␈α
which,␈α
in␈α∞retrospect,␈α
LISP␈α
could␈α∞have␈α
done␈α
better.␈α∞ By␈α
way␈α
of␈α∞review␈α
we␈α
sketch␈α∞the␈α
basic
␈↓ ↓H␈↓LISP evaluator of page 4.5: ␈↓αeval␈↓ plus the additional artifacts for ␈↓αlabel and function␈↓.

␈↓ ↓H␈↓There␈αare␈αtwo␈αarguments␈αto␈α␈↓αeval␈↓:␈αa␈α␈↓↓form␈↓␈↓π 87␈↓,␈αthat␈αis,␈αan␈αexpression␈αwhich␈αcan␈αbe␈αevaluated;␈αand␈αan
␈↓ ↓H␈↓association␈α⊃list␈α⊂or␈α⊃␈↓↓symbol␈α⊂table␈↓.␈α⊃If␈α⊂the␈α⊃form␈α⊃is␈α⊂a␈α⊃constant,␈α⊂return␈α⊃that␈α⊂form.␈α⊃If␈α⊂the␈α⊃form␈α⊃is␈α⊂a
␈↓ ↓H␈↓variable,␈α
find␈α
the␈α
value␈α
of␈α
the␈α
variable␈α
in␈α
the␈α
current␈α
environment.␈α
 If␈α
the␈α
form␈α
is␈α∞a␈α
conditional
␈↓ ↓H␈↓expression, then evaluate it according to the semantics of conditional expressions.

␈↓ ↓H␈↓The␈α
form␈α
might␈α
also␈α
be␈α
a␈α
functional␈αargument.␈α
In␈α
this␈α
case␈α
evaluation␈α
consists␈α
of␈α
associating␈αthe
␈↓ ↓H␈↓current␈αenvironment␈αwith␈αthe␈αfunction␈αand␈αreturning␈αthat␈αconstruct␈αas␈αvalue;␈αin␈αLISP␈αthis␈αis␈αdone
␈↓ ↓H␈↓with␈α∂the␈α∂␈↓αfunarg␈↓␈α∂device.␈α∞ Any␈α∂other␈α∂form␈α∂seen␈α∞by␈α∂␈↓αeval␈↓␈α∂is␈α∂assumed␈α∞to␈α∂be␈α∂a␈α∂function␈α∂followed␈α∞by
␈↓ ↓H␈↓arguments.␈α∂ The␈α∂arguments␈α∂are␈α∂evaluated␈α∂from␈α∂left-to-right␈α∂and␈α∂the␈α∂function␈α∂is␈α∂then␈α∂applied␈α∂to
␈↓ ↓H␈↓these arguments.

␈↓ ↓H␈↓The␈α∞part␈α
of␈α∞the␈α∞evaluator␈α
which␈α∞handles␈α∞function␈α
application␈α∞is␈α∞called␈α
␈↓αapply␈↓.␈α∞ ␈↓αapply␈↓␈α∞takes␈α
three
␈↓ ↓H␈↓arguments:␈α
a␈α
function,␈α
a␈α
list␈α
of␈α
evaluated␈α
arguments,␈α
and␈α
the␈α
current␈α
symbol␈α
table.␈α
If␈α
the␈αfunction␈α
is
␈↓ ↓H␈↓one␈α∂of␈α∂the␈α∂five␈α∞LISP␈α∂primitives␈α∂then␈α∂the␈α∞appropriate␈α∂action␈α∂is␈α∂carried␈α∞out.␈α∂If␈α∂the␈α∂function␈α∂is␈α∞a
␈↓ ↓H␈↓λ-expression␈α∂then␈α∂bind␈α⊂the␈α∂formal␈α∂parameters␈α∂(the␈α⊂λ-variables)␈α∂to␈α∂the␈α∂evaluated␈α⊂arguments␈α∂and
␈↓ ↓H␈↓evaluate␈αthe␈αbody␈αof␈αthe␈αfunction.␈αThe␈αfunction␈αmight␈αalso␈αbe␈αthe␈αresult␈αof␈αa␈αfunctional␈αargument
␈↓ ↓H␈↓binding;␈αin␈αthis␈αcase␈αapply␈αthe␈αfunction␈αto␈αthe␈αarguments␈αin␈αthe␈αsaved␈αenvironment␈αrather␈αthan␈αin
␈↓ ↓H␈↓the␈α⊂current␈α∂environment.␈α⊂ If␈α⊂we␈α∂are␈α⊂applying␈α⊂the␈α∂␈↓αlabel␈↓␈α⊂operator,␈α⊂recalling␈α∂page 127,␈α⊂we␈α⊂build␈α∂a

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 87␈↓␈α⊃throughout␈α⊃this␈α⊃section␈α⊃we␈α⊃will␈α⊃say␈α⊃"form",␈α⊃"variable",␈α⊃"λ-expression",␈α⊃etc.␈α⊃ rather␈α⊃than␈α⊃"an
␈↓ ↓H␈↓S-expression␈α⊃representation␈α⊂of␈α⊃a"␈α⊂...␈α⊃"form",␈α⊂"variable",␈α⊃"λ-expression",␈α⊂etc.␈α⊃No␈α⊃confusion␈α⊂should
␈↓ ↓H␈↓result, but remember that we ␈↓↓are␈↓ speaking imprecisely.
␈↓ ↓H␈↓␈↓↓4.22␈↓ πfRapprochement: In retrospect     175␈↓


␈↓ ↓H␈↓␈↓αfunarg␈↓-triple␈α⊂and␈α⊂new␈α⊂environment␈α⊃such␈α⊂that␈α⊂the␈α⊂environment␈α⊂bound␈α⊃in␈α⊂the␈α⊂triple␈α⊂is␈α⊃the␈α⊂new
␈↓ ↓H␈↓environment. We then apply the function to the arguments in this new environment.

␈↓ ↓H␈↓If␈αthe␈α
function␈αhas␈αa␈α
name␈αwe␈αlook␈α
up␈αthat␈αname␈α
in␈αthe␈αcurrent␈α
environment.␈α Currently␈αwe␈α
expect
␈↓ ↓H␈↓that␈α∂value␈α∂to␈α∂be␈α∂a␈α∂λ-expression,␈α∂which␈α∂is␈α∂then␈α∂applied.␈α∂However,␈α∂since␈α∂function␈α∂names␈α∂are␈α∞just
␈↓ ↓H␈↓variables,␈αthere␈αis␈αno␈αreason␈αthat␈αthe␈αvalue␈αof␈αa␈αfunction␈αname␈αcould␈αnot␈αbe␈αa␈αsimple␈αvalue,␈αsay␈αan
␈↓ ↓H␈↓atom,␈α⊃and␈α⊃␈↓↓that␈↓␈α⊃value␈α⊃can␈α⊂be␈α⊃applied␈α⊃to␈α⊃the␈α⊃arguments,␈α⊂etc.␈α⊃ Indeed␈α⊃examination␈α⊃of␈α⊃␈↓αapply␈↓␈α⊂on
␈↓ ↓H␈↓page 101␈α
will␈αshow␈α
that␈α␈↓αapply[X; ((A␈α
B)) ; ((X . CAR) ... )]␈↓␈α␈↓↓will␈↓␈α
be␈αhandled␈α
correctly.␈α The␈α
obvious
␈↓ ↓H␈↓extension␈αof␈αthis␈αidea␈αis␈αto␈αallow␈αa␈α␈↓↓computed␈αfunction␈↓.␈αThat␈αis,␈αif␈αthe␈αfunction␈αpassed␈αto␈α␈↓αapply␈↓␈αis
␈↓ ↓H␈↓not␈αrecognized␈αas␈αone␈αof␈αthe␈αpreceding␈αcases,␈αthen␈αevaluate␈αthe␈αfunction-part␈αuntil␈αit␈αis␈αrecognized.
␈↓ ↓H␈↓Thus we will allow such forms as:

␈↓ ↓H␈↓α␈↓ ∧β((CAR (QUOTE (CAR (A . B)))) (QUOTE (A . B)))
␈↓ ↓H␈↓The addition of computed functions modifies ␈↓αapply␈↓ (page 101) slightly:

␈↓ ↓H␈↓αapply <= λ[[fn;args,environ]
␈↓ ↓H␈↓α␈↓ β([iscar[fn] → car[arg␈↓β1␈↓α[args]];
␈↓ ↓H␈↓α␈↓ β( iscons[fn] → cons[arg␈↓β1␈↓α[args];arg␈↓β2␈↓α[args]];
␈↓ ↓H␈↓α␈↓ β(        ...                ...
␈↓ ↓H␈↓α␈↓ β( islambda[fn] → eval[body[fn];pairlis[vars[fn];args;environ]];
␈↓ ↓H␈↓α␈↓ β( ␈↓
t␈↓α → apply[eval[fn;environ];args;environ] ]]

␈↓ ↓H␈↓What␈α∞conceptual␈α∞difficulties␈α
are␈α∞present␈α∞in␈α
LISP␈α∞evaluation?␈α∞ We␈α
have␈α∞seen␈α∞some␈α
computational
␈↓ ↓H␈↓schemes␈α⊃which␈α⊃will␈α∩give␈α⊃values␈α⊃when␈α⊃LISP's␈α∩call-by-value␈α⊃does␈α⊃not␈α⊃terminate.␈α∩ Whether␈α⊃these
␈↓ ↓H␈↓schemes␈α∞are␈α∞better␈α
is␈α∞a␈α∞debatable␈α
point.␈α∞ Programmers␈α∞tend␈α
to␈α∞think␈α∞"call-by-value",␈α
but␈α∞it␈α∞is␈α
not
␈↓ ↓H␈↓clear whether that is habit and training or a fundamental point of view towards computation.

␈↓ ↓H␈↓A␈α∩more␈α∩immediate␈α⊃problem␈α∩is␈α∩involved␈α∩with␈α⊃"<=".␈α∩We␈α∩were␈α⊃able␈α∩to␈α∩give␈α∩counterexamples␈α⊃to
␈↓ ↓H␈↓interpreting:

␈↓ ↓H␈↓␈↓ ∧⎇␈↓αf <= λ[[x]␈↓λx␈↓] as ␈↓αf ← quote[λ[[x]␈↓λx␈↓α]].

␈↓ ↓H␈↓The␈α∞discussion␈α∞of␈α∞binding␈α∞and␈α∂environments␈α∞made␈α∞␈↓αf ← function[λ[[x]␈↓λx␈↓α]]␈↓␈α∞a␈α∞more␈α∂likely␈α∞candidate;
␈↓ ↓H␈↓however this interpretation is also not adequate.

␈↓ ↓H␈↓We attempt to implement ␈↓ ∧5␈↓αfact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;fact[x-1]]]]␈↓ as:

␈↓ ↓H␈↓α␈↓ ∧rfact ← function[λ[[x] ... fact[x-1] ...]
␈↓ ↓H␈↓Consider an initial environment with ␈↓αfact␈↓ defined:

␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓
␈↓ ↓H␈↓␈↓↓176  Evaluation␈↓ $4.22␈↓


␈↓ ↓H␈↓We␈α∪will␈α∩demonstrate␈α∪the␈α∩inadequacy␈α∪of␈α∩two␈α∪natural␈α∩interpretations␈α∪of␈α∩function␈α∪values:␈α∩direct
␈↓ ↓H␈↓assignment␈α∞of␈α
value,␈α∞and␈α∞assignment␈α
of␈α∞␈↓αfunarg␈↓.␈α
 We␈α∞execute␈α∞␈↓αfoo␈α
←␈α∞fact␈↓␈α
and␈α∞␈↓αbaz␈α∞←␈α
function[fact]␈↓,
␈↓ ↓H␈↓giving:

␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓αfoo␈↓␈↓ ¬X|␈↓αλ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓αbaz␈↓␈↓ ¬X|␈↓αfact : ␈↓E␈↓βi␈↓


␈↓ ↓H␈↓Things␈αdon't␈αlook␈αquite␈αright;␈αthe␈α"intent"␈αof␈αboth␈α␈↓αfoo ← fact␈↓␈αand␈α␈↓αbaz ← function[fact]␈↓␈αwas␈αto␈αmake
␈↓ ↓H␈↓␈↓αfoo␈↓␈α
and␈α
␈↓αbaz␈↓␈α
synonymous␈α
with␈α
␈↓αfact␈↓.␈α
That␈α
clearly␈α
is␈α
not␈α
the␈α
case␈α
though␈α
the␈α
right␈α
thing␈α
happens␈αif␈α
we
␈↓ ↓H␈↓were␈α
now␈α
to␈α
evaluate␈α
an␈αexpression␈α
involving␈α
␈↓αfoo␈↓␈α
or␈α
␈↓αbaz␈↓.␈α
The␈αproblem␈α
is␈α
that␈α
it␈α
happens␈α
for␈αthe
␈↓ ↓H␈↓wrong␈α
reason␈αeven␈α
though␈α
the␈αoccurrence␈α
of␈α
␈↓αfact␈↓␈αin␈α
the␈α
body␈αof␈α
␈↓αfoo␈↓␈α
will␈αfind␈α
the␈α
right␈αdefinition␈α
of
␈↓ ↓H␈↓␈↓αfact␈↓; an application of ␈↓αbaz␈↓ will find the definition of ␈↓αfact␈↓.

␈↓ ↓H␈↓One more step will lead to disaster: ␈↓αfact ← quote[λ[[x]x]]␈↓.
␈↓ ↓H␈↓␈↓ ¬_␈↓ ¬XE␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_E␈↓βc␈↓␈↓ ¬X|␈↓ ε_E␈↓βa␈↓
␈↓ ↓H␈↓␈↓ ¬____________
␈↓ ↓H␈↓␈↓ ¬_␈↓αfact␈↓␈↓ ¬X|␈↓αλ[[x]x]
␈↓ ↓H␈↓α␈↓ ¬_foo␈↓ ¬X|λ[[x] ...fact[x-1]] : ␈↓E␈↓βi␈↓
␈↓ ↓H␈↓␈↓ ¬_␈↓αbaz␈↓␈↓ ¬X|␈↓αfact : ␈↓E␈↓βi␈↓

␈↓ ↓H␈↓Now␈α
we␈α
have␈α
really␈α
lost.␈α
Though␈α
it␈α
is␈α
perfectly␈α
reasonable␈α
to␈α
redefine␈α
␈↓αfact␈↓␈α
-- it␈α
is␈α
only␈α∞a␈α
name --
␈↓ ↓H␈↓our␈α
intent␈αwas␈α
to␈α
keep␈α␈↓αbaz␈↓␈α
and␈α
␈↓αfoo␈↓␈αas␈α
realizations␈α
of␈αthe␈α
factorial␈α
function.␈αThis␈α
intent␈α
has␈αnot␈α
been
␈↓ ↓H␈↓maintained.

␈↓ ↓H␈↓␈↓ ∧ ␈↓αfact␈↓ <= ␈↓αλ[[x] ...fact[x-1]]␈↓ is quite different from:

␈↓ ↓H␈↓␈↓ ¬;␈↓αfoo␈↓ <= ␈↓αλ[[x] ...fact[x-1]].

␈↓ ↓H␈↓To␈α∞attempt␈α∞to␈α∞understand␈α∂what␈α∞is␈α∞happening␈α∞we␈α∞look␈α∂at␈α∞assignments␈α∞to␈α∞␈↓↓simple␈↓␈α∂variables␈α∞rather
␈↓ ↓H␈↓than␈α
functional␈αvariables.␈α
It␈αis␈α
clear␈α
how␈αthe␈α
environment␈αshould␈α
change␈αduring␈α
the␈α
execution␈αof
␈↓ ↓H␈↓the sequence:

␈↓ ↓H␈↓α␈↓ ¬_x ← 3
␈↓ ↓H␈↓α␈↓ ¬_y ← x
␈↓ ↓H␈↓α␈↓ ¬_x ← 4

␈↓ ↓H␈↓Let's try something slightly more complicated:

␈↓ ↓H␈↓α␈↓ ¬_x ← 1
␈↓ ↓H␈↓α␈↓ ¬_x ← x␈↓π2␈↓α - 6
␈↓ ↓H␈↓␈↓↓4.22␈↓ πfRapprochement: In retrospect     177␈↓


␈↓ ↓H␈↓Here␈α⊃we␈α⊃simply␈α⊃assign␈α⊃␈↓α1␈↓␈α⊃to␈α⊃␈↓αx␈↓;␈α⊃then␈α⊃while␈α⊂we␈α⊃are␈α⊃evaluating␈α⊃the␈α⊃right␈α⊃hand␈α⊃side␈α⊃of␈α⊃the␈α⊂next
␈↓ ↓H␈↓statement,␈α∞we␈α∞look␈α∞up␈α∞the␈α∞current␈α∞value␈α∞of␈α∞␈↓αx␈↓,␈α∞perform␈α∞the␈α∞appropriate␈α∞computations,␈α∂and␈α∞finally
␈↓ ↓H␈↓assign␈α⊃the␈α⊃value␈α⊃␈↓α-5␈↓␈α⊃to␈α⊃␈↓αx␈↓.␈α⊃Compare␈α⊃␈↓↓this␈↓␈α∩to␈α⊃the␈α⊃␈↓αfact␈↓␈α⊃definition;␈α⊃there␈α⊃we␈α⊃made␈α⊃a␈α⊃point␈α∩of␈α⊃not
␈↓ ↓H␈↓"evaluating" the name ␈↓αfact␈↓ in the right hand side of "<=".

␈↓ ↓H␈↓Notice␈α⊂that␈α⊂␈↓↓after␈↓␈α∂an␈α⊂assignment␈α⊂like␈α⊂␈↓αy ← x␈↓␈α∂has␈α⊂been␈α⊂executed,␈α∂then␈α⊂equality␈α⊂holds␈α⊂between␈α∂the
␈↓ ↓H␈↓left-hand␈αand␈α
right-hand␈αquantities␈α
␈↓π 88␈↓.␈α Let's␈αlook␈α
more␈αclosely␈α
at␈αthe␈α
relationship␈αbetween␈α"←"␈α
and
␈↓ ↓H␈↓"=".␈α
 Consider␈α
␈↓αx ← x␈↓π2␈↓α - 6␈↓.␈α
After␈α∞the␈α
assignment␈α
is␈α
made,␈α
equality␈α∞does␈α
␈↓↓not␈↓␈α
hold␈α
between␈α∞left-␈α
and
␈↓ ↓H␈↓right-hand␈αsides.␈α Now␈α
consider␈α␈↓αx = x␈↓π2␈↓α - 6␈↓.␈α Interpreting␈α
this␈αexpression␈αas␈α
an␈αequation,␈αnot␈α
as␈αan
␈↓ ↓H␈↓expression␈αwhose␈αvalue␈αis␈αtrue␈αor␈αfalse␈αdepending␈αon␈αthe␈αcurrent␈αvalue␈αof␈α␈↓αx␈↓,␈αwe␈αfind␈αtwo␈αsolutions:
␈↓ ↓H␈↓let␈α␈↓αx␈↓␈αhave␈αvalue␈α␈↓α-2␈↓␈αor␈α␈↓α3␈↓.␈α If␈αwe␈αexamine␈αour␈α"definition"␈αof␈α␈↓αfact␈↓␈αin␈α␈↓↓this␈↓␈αlight,␈αinterpreting␈α"<="␈αas
␈↓ ↓H␈↓being␈αrelated␈αto␈α
"="␈αrather␈αthan␈α"←",␈α
then␈αwe␈αare␈αfaced␈α
with␈αan␈αequation␈αto␈α
be␈αsolved,␈αonly␈αnow␈α
the
␈↓ ↓H␈↓form␈α∪of␈α∩the␈α∪solution␈α∩is␈α∪a␈α∪␈↓↓function␈↓␈α∩which␈α∪satisfies␈α∩the␈α∪equation.␈α∩There␈α∪are␈α∪frequently␈α∩many
␈↓ ↓H␈↓solutions;␈α
there␈α
may␈αbe␈α
no␈α
solutions.␈α In␈α
our␈α
case␈α
there␈αis␈α
at␈α
least␈αone␈α
solution:␈α
the␈α
usual␈αfactorial
␈↓ ↓H␈↓function.  So what we ␈↓↓should␈↓ write is something more like:

␈↓ ↓H␈↓α␈↓ β_fact ← ␈↓a solution to the equation␈↓α: f = λ[[x][x=0 → 1; ␈↓
t␈↓α → *[x;f[x-1]]]].

␈↓ ↓H␈↓That␈α∞is,␈α∞the␈α∞␈↓↓real␈↓␈α∞intent␈α∞of␈α∞the␈α∞recursive␈α∂definition␈α∞of␈α∞␈↓αfact␈↓␈α∞was␈α∞to␈α∞define␈α∞a␈α∞function␈α∞to␈α∂effect␈α∞the
␈↓ ↓H␈↓computation␈α∞of␈α
factorial␈α∞and␈α∞then␈α
to␈α∞␈↓↓name␈↓␈α∞that␈α
function␈α∞␈↓αfact␈↓.␈α∞Questions␈α
of␈α∞when␈α∞solutions␈α
exist,
␈↓ ↓H␈↓how many, and which are the "best" solutions is a topic of much current research [Man 74].

␈↓ ↓H␈↓LISP␈αhas␈αtwo␈αways␈αof␈α
assigning␈αvalues␈αto␈αfunctions:␈α␈↓αlabel␈↓␈αand␈α
<=.␈α The␈αuse␈αof␈α␈↓αlabel␈↓␈αmanufactures␈α
a
␈↓ ↓H␈↓new␈α∃"knotted"␈α∃environment␈α∃but␈α∃does␈α∃not␈α∃always␈α∃find␈α∃the␈α∃mimimal␈α∃solution␈α∃[Gor 73].␈α∀ The
␈↓ ↓H␈↓evaluation␈α∂of␈α∂␈↓αf <= ␈↓λf␈↓␈α∂is␈α∂interpreted␈α∂as␈α∂␈↓αf ← quote[␈↓λf␈↓α]␈↓,␈α∞where␈α∂the␈α∂assignment␈α∂is␈α∂make␈α∂in␈α∂the␈α∞global
␈↓ ↓H␈↓environment.␈α LISP's␈α
solutions␈αare␈αsufficient␈α
for␈αmost␈αdefinitions,␈α
but␈αthis␈αdiscussion␈α
should␈αmake
␈↓ ↓H␈↓clear␈α↔the␈α↔necessity␈α⊗of␈α↔distinguishing␈α↔between␈α⊗assignment␈α↔and␈α↔equality;␈α↔unfortunately,␈α⊗many
␈↓ ↓H␈↓programming languages use notations which tend to obscure these differences.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I␈αThe␈α␈↓αfoo-fact-baz␈↓␈αexample␈αof␈α
this␈αsection␈αis␈αnot␈αdescribed␈αin␈α
LISP.␈αCan␈αyou␈αwrite␈αa␈αLISP␈α
program
␈↓ ↓H␈↓without ␈↓αprog␈↓s which will also exemplify this difficulty?



␈↓ ↓H␈↓␈↓ ¬,␈↓↓4.23  The LISP machine␈↓


␈↓ ↓H␈↓The␈α∂LISP␈α∂definitions␈α∞and␈α∂expression␈α∂which␈α∂we␈α∞have␈α∂been␈α∂writing␈α∞are␈α∂expressed␈α∂in␈α∂a␈α∞language

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 88␈↓ Indeed, a logic of programs due to C.A.R. Hoare exploits this fact in interpreting assignment.
␈↓ ↓H␈↓␈↓↓178  Evaluation␈↓ $4.23␈↓


␈↓ ↓H␈↓called␈α
the␈α
␈↓↓meta-language␈↓,␈α
and␈α
the␈α
LISP␈α
expressions␈α
called␈α
M-expressions␈α
or␈α
M-exprs.␈α∞ The␈α
most
␈↓ ↓H␈↓primitive␈α∂data␈α∂structures␈α∂of␈α⊂LISP␈α∂are␈α∂called␈α∂S-expressions.␈α∂ We␈α⊂have␈α∂seen␈α∂that␈α∂it␈α∂is␈α⊂possible␈α∂to
␈↓ ↓H␈↓represent␈α∂M-expressions␈α⊂as␈α∂S-expressions,␈α⊂and␈α∂indeed,␈α⊂that␈α∂significant␈α⊂results␈α∂are␈α⊂obtained␈α∂from
␈↓ ↓H␈↓such␈α⊂a␈α∂mapping.␈α⊂ The␈α∂␈↓↓programming␈α⊂language␈↓,␈α∂LISP,␈α⊂uses␈α∂the␈α⊂S-expr␈α∂translation␈α⊂of␈α⊂the␈α∂LISP
␈↓ ↓H␈↓algorithms.␈α∪ As␈α∀we␈α∪move␈α∀from␈α∪the␈α∀more␈α∪formal␈α∀aspects␈α∪of␈α∀LISP␈α∪to␈α∀the␈α∪practical␈α∀details␈α∪of
␈↓ ↓H␈↓implementations␈α⊗and␈α⊗applications,␈α⊗we␈α⊗should␈α⊗examine␈α∃some␈α⊗of␈α⊗the␈α⊗features␈α⊗of␈α⊗LISP␈α⊗as␈α∃a
␈↓ ↓H␈↓programming language.

␈↓ ↓H␈↓Since␈α∂proper␈α⊂input␈α∂to␈α⊂LISP␈α∂programs␈α⊂are␈α∂S-exprs␈α⊂and␈α∂since␈α⊂we␈α∂are␈α⊂writing␈α∂LISP␈α⊂programs␈α∂in
␈↓ ↓H␈↓S-expr␈α
form,␈α
then␈α
on␈α
input,␈αdata␈α
and␈α
program␈α
are␈α
indistinguishable␈αin␈α
format;␈α
i.e.,␈α
they␈α
are␈αboth
␈↓ ↓H␈↓binary␈α
trees.␈α
For␈α
evaluation,␈α∞programs␈α
must␈α
have␈α
a␈α
very␈α∞special␈α
structure,␈α
but␈α
program␈α∞and␈α
data
␈↓ ↓H␈↓are␈α
both␈α
S-exprs␈α
just␈α
as␈αin␈α
most␈α
hardware␈α
machines␈α
the␈αcontents␈α
of␈α
locations␈α
which␈α
contain␈αdata
␈↓ ↓H␈↓are␈α→indistinguishable␈α→in␈α_format␈α→from␈α→locations␈α→which␈α_contain␈α→instructions.␈α→ On␈α→a␈α_typical
␈↓ ↓H␈↓contemporary␈α∂machine␈α⊂it␈α∂is␈α⊂the␈α∂way␈α⊂in␈α∂which␈α⊂a␈α∂location␈α⊂is␈α∂accessed␈α⊂which␈α∂determines␈α⊂how␈α∂the
␈↓ ↓H␈↓contents␈α⊃of␈α⊃that␈α⊂location␈α⊃are␈α⊃interpreted.␈α⊃ If␈α⊂the␈α⊃central␈α⊃processor␈α⊂accesses␈α⊃the␈α⊃location␈α⊃via␈α⊂the
␈↓ ↓H␈↓program␈αcounter,␈αthe␈αcontents␈αare␈αassumed␈αto␈α
be␈αan␈αinstruction.␈α That␈αsame␈αlocation␈αcan␈αusually␈α
be
␈↓ ↓H␈↓accessed␈αas␈αpart␈αof␈αa␈α
data␈αmanipulating␈αinstruction.␈α In␈αthat␈α
case,␈αthe␈αcontents␈αare␈αsimply␈α
taken␈αto
␈↓ ↓H␈↓be␈α
data.␈α∞ LISP␈α
is␈α∞one␈α
of␈α
the␈α∞few␈α
high-level␈α∞languages␈α
which␈α
also␈α∞has␈α
this␈α∞property.␈α
 It␈α∞gives␈α
the
␈↓ ↓H␈↓programmer exceptional power.

␈↓ ↓H␈↓The␈αcentral␈αprocessor␈αof␈α
a␈α"LISP␈αmachine"␈αis␈α
␈↓αeval␈↓␈↓π 89␈↓.␈α If␈α␈↓αeval␈↓␈αreferences␈α
an␈αS-expr␈αvia␈αits␈α
"program
␈↓ ↓H␈↓counter",␈α∞then␈α∞that␈α
S-expr␈α∞is␈α∞decoded␈α
via␈α∞the␈α∞internals␈α∞of␈α
␈↓αeval␈↓.␈α∞ If␈α∞an␈α
S-expr␈α∞is␈α∞referenced␈α∞as␈α
an
␈↓ ↓H␈↓argument,␈αthen␈αit␈αis␈α
taken␈αas␈αdata␈↓π 90␈↓.␈α The␈α
identity␈αof␈αprogram␈αand␈α
data␈αis␈αnot␈αfixed␈α
even␈αwithin
␈↓ ↓H␈↓the␈α⊂execution;␈α⊂a␈α∂LISP␈α⊂program␈α⊂can␈α⊂␈↓αcons␈↓ up␈α∂a␈α⊂data␈α⊂structure␈α⊂which␈α∂can␈α⊂then␈α⊂be␈α⊂interpreted␈α∂as
␈↓ ↓H␈↓program␈α
either␈α
by␈α
explicitly␈α
calling␈α
␈↓αeval␈↓␈α
or␈α
implicitly␈α
by␈α
appearing␈α
in␈α
the␈α
function-position␈α
of␈α
an
␈↓ ↓H␈↓application.

␈↓ ↓H␈↓The␈αsimplest␈αway␈αto␈αcommunicate␈αwith␈αsuch␈αa␈αmachine␈αis␈αto␈αread␈αan␈αS-expr␈αtranslation␈αof␈αa␈αLISP
␈↓ ↓H␈↓expression␈α
into␈α
memory,␈α
evaluate␈α
the␈α
expression,␈α
and␈α
print␈α
the␈α
result.␈α
Several␈α
implementations␈αof
␈↓ ↓H␈↓LISP use a variant of this "␈↓αread-eval-print␈↓" loop:

␈↓ ↓H␈↓α␈↓ ¬_prog[[]
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ¬Xa␈↓ ¬xprint[eval[read[];( )]];
␈↓ ↓H␈↓α␈↓ ¬_␈↓ ¬X␈↓ ¬xgo[a]].

␈↓ ↓H␈↓Note␈α
the␈α
similarity␈α
to␈α
␈↓αloop␈↓␈α
on␈α
page 158.␈α
 The␈αdetails␈α
of␈α
the␈α
input␈α
and␈α
output␈α
operations␈α
in␈αLISP
␈↓ ↓H␈↓are discussed in the next chapter.

␈↓ ↓H␈↓List-notation␈α⊂is␈α⊂a␈α∂recognizable␈α⊂input␈α⊂and␈α⊂the␈α∂output␈α⊂from␈α⊂LISP␈α∂programs␈α⊂will␈α⊂be␈α⊂converted␈α∂to
␈↓ ↓H␈↓list-notation␈α
wherever␈α
possible.␈α
 But␈αthat's␈α
where␈α
things␈α
stand.␈αLISP␈α
will␈α
allow␈α
you␈α
to␈αmanipulate

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 89␈↓␈α∞Several␈α∂"LISP␈α∞machines"␈α∂have␈α∞been␈α∞proposed␈α∂or␈α∞actually␈α∂built␈α∞including:␈α∂[Got xx],␈α∞[Gre 74],
␈↓ ↓H␈↓[Deu 73], and [Bar 71]

␈↓ ↓H␈↓␈↓π 90␈↓ This goes for ␈↓αfunarg␈↓s as well: until they're applied, they're data.
␈↓ ↓H␈↓␈↓↓4.23␈↓ λzThe LISP machine     179␈↓


␈↓ ↓H␈↓the␈α␈↓↓representation␈↓␈αof␈αthe␈αlists.␈α The␈αLISP␈αS-expr␈αoperations␈αlike␈α␈↓αcar,␈αcdr␈↓,␈αand␈α␈↓αcons␈↓␈αoperate␈αwithout
␈↓ ↓H␈↓complaint␈α⊃on␈α⊃lists,␈α⊃even␈α⊂though␈α⊃we␈α⊃have␈α⊃repeatedly␈α⊂said␈α⊃that␈α⊃these␈α⊃functions␈α⊃are␈α⊂S-expression
␈↓ ↓H␈↓functions.␈α∂ LISP's␈α∂attitude␈α∂to␈α∞␈↓αcar␈↓␈α∂and␈α∂␈↓αcdr␈↓␈α∂is␈α∞similar␈α∂to␈α∂its␈α∂treatment␈α∞of␈α∂␈↓αgo␈↓s␈α∂and␈α∂labels:␈α∂these␈α∞are
␈↓ ↓H␈↓useful␈α⊃primitives␈α⊃out␈α⊃of␈α⊃which␈α⊃to␈α⊃build␈α⊃"real"␈α⊃tools;␈α⊃either␈α⊃real␈α⊃data␈α⊃structures␈α⊃or␈α⊃real␈α⊂control
␈↓ ↓H␈↓structures.

␈↓ ↓H␈↓However␈α⊃the␈α⊃effect␈α⊃of␈α⊃this␈α⊃behavior␈α⊃is␈α∩to␈α⊃present␈α⊃the␈α⊃potential␈α⊃LISP␈α⊃user␈α⊃with␈α∩an␈α⊃incredible
␈↓ ↓H␈↓potentiality␈α∃for␈α∃generating␈α⊗programming␈α∃errors.␈α∃ It␈α∃also␈α⊗removes␈α∃from␈α∃the␈α∃user␈α⊗a␈α∃powerful
␈↓ ↓H␈↓debugging␈αtool.␈αIf␈αwe␈αwrite␈αprograms␈αsuch␈αthat␈αthe␈αtype␈αof␈αeach␈αdata␈αobject␈αmust␈αbe␈αgiven␈↓π 91␈↓,␈αand
␈↓ ↓H␈↓if␈αwe␈αwrite␈αeach␈αfunction␈αsuch␈αthat␈αthe␈α
process␈αof␈αbinding␈αarguments␈αto␈αvalues␈αmust␈αcheck␈αthat␈α
the
␈↓ ↓H␈↓type␈α∞of␈α∞the␈α∞actual␈α∞parameter␈α∞agrees␈α∞with␈α∞the␈α∞type␈α∞of␈α∞the␈α∞parameter␈α∞of␈α∞the␈α∞function,␈α∞then␈α∂a␈α∞very
␈↓ ↓H␈↓large␈α
number␈α
of␈α
programming␈α
errors␈α
can␈α
be␈α
located␈α
almost␈α
as␈α
soon␈α
as␈α
they␈α
occur.␈α
 You␈α
can␈αthink␈α
of
␈↓ ↓H␈↓the␈αparameter-passing␈αmechanism␈αas␈αsort␈αof␈αa␈α"fire-wall",␈αwhich␈αwill␈αattempt␈αto␈αcontain␈αthe␈αdeviant
␈↓ ↓H␈↓behavior within the particular function.

␈↓ ↓H␈↓Any␈αfunction␈α
which␈αgets␈αcalled␈α
has␈αa␈αright␈α
to␈αexpect␈αthat␈α
it␈αwill␈αbe␈α
called␈αwith␈α
reasonable␈αvalues.
␈↓ ↓H␈↓Part␈αof␈αbeing␈αreasonable␈αis␈αhaving␈αthe␈αcorrect␈αnumber␈αof␈αarguments␈αgiven␈αto␈αit;␈α␈↓αcons[A; B; C]␈↓␈αis␈αas
␈↓ ↓H␈↓bad␈α∞as␈α
␈↓αcons[A]␈↓.␈α∞Part␈α
of␈α∞being␈α
reasonable␈α∞is␈α∞having␈α
the␈α∞right␈α
kind␈α∞of␈α
arguments;␈α∞we␈α∞don't␈α
expect
␈↓ ↓H␈↓reasonable␈α
results␈α
from␈α
␈↓αsub1[A]␈↓.␈α
 We␈α
should␈α
not␈α
expect␈α
that␈α
the␈α
functions␈α
are␈α
sufficiently␈α
omniscient
␈↓ ↓H␈↓to␈αconvert␈αan␈αargument␈αof␈αthe␈αwrong␈αkind␈αinto␈αa␈αproper␈αone.␈α If␈αa␈αfunction␈αis␈αwritten␈αto␈αexpect␈αan
␈↓ ↓H␈↓argument␈α
of␈α
type␈α
␈↓αpolynomial␈↓␈α
then␈α
it␈α
should␈α∞complain␈α
if␈α
it␈α
receives␈α
an␈α
argument␈α
of␈α
type␈α∞␈↓αlist␈↓␈α
even
␈↓ ↓H␈↓though the current representation for polynomials might be special instances of lists.

␈↓ ↓H␈↓Many␈α
current␈α
languages␈α
do␈α
indeed␈α
offer␈α
such␈α
omniscience.␈α
Fortran␈α
calls␈α
this␈α
service␈α"conversion";
␈↓ ↓H␈↓Algol68␈α
calls␈α∞it␈α
␈↓↓"coercion"␈↓.␈α
 However␈α∞if␈α
each␈α
function␈α∞accepts␈α
whatever␈α
argument␈α∞it␈α
is␈α∞given␈α
and
␈↓ ↓H␈↓attempts␈α
to␈α
use␈α
it␈αin␈α
its␈α
computation,␈α
then␈αthe␈α
first␈α
indication␈α
of␈αtrouble␈α
will␈α
occur␈α
when␈αa␈α
primitive
␈↓ ↓H␈↓function␈αis␈αcalled␈αand␈αcauses␈αsome␈αerror␈αdeep␈αwithin␈αthe␈αimplementation.␈α Typically␈αthis␈αindication
␈↓ ↓H␈↓of␈α
error␈αis␈α
way␈αpast␈α
the␈αactual␈α
source␈αof␈α
the␈α
difficulty.␈αThe␈α
alternative␈αis␈α
to␈αexplicitly␈α
code␈αtests␈α
into
␈↓ ↓H␈↓the␈α∞entry␈α
code␈α∞of␈α
each␈α∞function␈α
definition;␈α∞but␈α
that's␈α∞an␈α
expensive␈α∞use␈α
of␈α∞the␈α∞programmer's␈α
time
␈↓ ↓H␈↓and␈α
the␈α
computation␈α
time.␈α
 What␈α
typically␈α
happens␈αis␈α
that␈α
the␈α
tests␈α
are␈α
left␈α
out␈α
and␈αdebugging␈α
soon
␈↓ ↓H␈↓follows.

␈↓ ↓H␈↓As␈αwith␈αmost␈αareas␈αof␈αprogramming,␈αcoercion␈α
is␈αnot␈αa␈αblack-or-white␈αissue.␈α A␈αstrong␈αtype␈α
structure
␈↓ ↓H␈↓can␈α∀hinder␈α∪as␈α∀well␈α∀as␈α∪help.␈α∀Requiring␈α∪explicit␈α∀declarations␈α∀and␈α∪directions␈α∀for␈α∀conversion␈α∪is
␈↓ ↓H␈↓frequently␈α⊃annoying.␈α⊃Indeed␈α⊃several␈α⊃important␈α⊃programs␈α⊃should␈α⊃be␈α⊃type-free.␈α⊃In␈α∩particular␈α⊃the
␈↓ ↓H␈↓debugging␈α
programs␈α
must␈α∞be␈α
able␈α
to␈α
freely␈α∞access␈α
all␈α
parts␈α
of␈α∞the␈α
representation␈α
of␈α∞program␈α
and
␈↓ ↓H␈↓data without regard for type.

␈↓ ↓H␈↓LISP's␈α
position␈αis␈α
that␈α
it␈αis␈α
the␈αuser's␈α
responsibility␈α
to␈αhandle␈α
all␈αtype␈α
restrictions␈α
by␈αprogrammed
␈↓ ↓H␈↓tests.␈α∂ LISP␈α∂has␈α∂no␈α∂capability␈α∂to␈α∂define␈α∂and␈α∂maintain␈α∂abstract␈α∂data␈α∂structures.␈α⊂ However␈α∂people
␈↓ ↓H␈↓have␈α∞begun␈α∞investigations␈α
of␈α∞"typed␈α∞LISP"␈α
[Car 76],␈α∞and␈α∞some␈α
implementations␈α∞of␈α∞LISP␈α
[Int 75]
␈↓ ↓H␈↓give some aids in constructing and maintaining typed data structures.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 91␈↓␈αFor␈α
example,␈αin␈α
Section 3.3␈αthe␈α
types␈αof␈αthe␈α
arguments␈αto␈α
␈↓αdiff␈↓␈αshould␈α
be␈α<poly>␈αand␈α
<variable>,
␈↓ ↓H␈↓␈↓↓not␈↓ list and atom.
␈↓ ↓H␈↓␈↓↓180  Evaluation␈↓ $4.23␈↓


␈↓ ↓H␈↓Symbolic␈αexpressions␈αare␈α
the␈αonly␈αreal␈α
data␈αstructure;␈αwe␈α
almost␈αhave␈αsequences␈α
as␈αa␈αdata␈α
structure,
␈↓ ↓H␈↓and␈α⊃the␈α∩necessary␈α⊃ingredients␈α⊃are␈α∩there␈α⊃to␈α⊃build␈α∩abstract␈α⊃data␈α⊃structures.␈α∩But␈α⊃the␈α∩question␈α⊃of
␈↓ ↓H␈↓integrity␈α∞in␈α
using␈α∞such␈α∞defined␈α
data␈α∞structures␈α∞is␈α
left␈α∞totally␈α
in␈α∞the␈α∞hands␈α
of␈α∞the␈α∞programmer.␈α
In
␈↓ ↓H␈↓summary,␈αLISP␈αis␈αan␈αexcellent␈αtool␈αfor␈α
building␈αmore␈αcomplex␈αsystems.␈αThis␈αis␈αone␈αof␈α
the␈αreasons
␈↓ ↓H␈↓that LISP has maintained its position as the "machine language" for artificial intelligence.
␈↓ ↓H␈↓␈↓↓5.␈↓ 	/static structure     181␈↓


␈↓ ↓H␈↓␈↓ ¬}␈↓↓SECTION 5

␈↓ ↓H␈↓↓␈↓ β_IMPLEMENTATION OF THE STATIC STRUCTURE OF LISP␈↓






␈↓ ↓H␈↓␈↓ ¬`␈↓↓5.1  Introduction␈↓


␈↓ ↓H␈↓The␈αmaterial␈αin␈αthe␈α
previous␈αchapters␈αhas␈αbeen␈αrather␈α
abstract.␈α This␈αchapter␈αbegins␈α
a␈αdiscussion
␈↓ ↓H␈↓of␈α⊗the␈α⊗actual␈α⊗mechanisms␈α⊗which␈α⊗underlie␈α∃a␈α⊗typical␈α⊗implementation␈α⊗of␈α⊗LISP.␈α⊗However␈α∃the
␈↓ ↓H␈↓importance␈α⊃of␈α∩the␈α⊃techniques␈α∩we␈α⊃will␈α∩describe␈α⊃extends␈α∩far␈α⊃beyond␈α∩the␈α⊃implementation␈α∩of␈α⊃this
␈↓ ↓H␈↓particular␈α
language.␈α
Most␈α
of␈α
the␈α
ideas␈α
involved␈α
in␈α
our␈α
implementation␈α
are␈α
now␈α
considered␈α
standard
␈↓ ↓H␈↓system␈α∂programming␈α⊂techniques␈α∂and␈α∂are␈α⊂common␈α∂tools␈α∂in␈α⊂language␈α∂design.␈α∂LISP␈α⊂is␈α∂particularly
␈↓ ↓H␈↓well-suited␈α∞to␈α∞the␈α∂task␈α∞of␈α∞explicating␈α∂these␈α∞ideas␈α∞since␈α∂many␈α∞find␈α∞their␈α∂origins␈α∞in␈α∞the␈α∂first␈α∞LISP
␈↓ ↓H␈↓implementation.

␈↓ ↓H␈↓We␈α↔will␈α↔begin␈α↔our␈α↔discussion␈α↔of␈α⊗implementation␈α↔with␈α↔an␈α↔analysis␈α↔of␈α↔storage␈α↔regimes␈α⊗for
␈↓ ↓H␈↓S-expressions.␈α
As␈α
with␈α
the␈α
more␈α
abstract␈α
discussions␈α
of␈α
representations,␈α
the␈α
"concrete"␈α
representation
␈↓ ↓H␈↓which␈α→we␈α→pick␈α→for␈α~our␈α→data␈α→structures␈α→(S-expressions)␈α~will␈α→have␈α→direct␈α→bearing␈α~on␈α→the
␈↓ ↓H␈↓implementation␈α∞of␈α∞the␈α∞primitive␈α
constructor␈α∞(␈↓αcons␈↓),␈α∞selectors␈α∞(␈↓αcar,␈α
cdr␈↓)␈α∞and␈α∞predicates␈α∞(␈↓αatom,␈α∞eq␈↓)␈α
of
␈↓ ↓H␈↓LISP.

␈↓ ↓H␈↓Since␈αwe␈α
are␈αnow␈αattempting␈α
to␈αbecome␈α
more␈αpractical,␈αwe␈α
must␈αworry␈αabout␈α
the␈αefficiency␈α
of␈αthe
␈↓ ↓H␈↓implementation␈α⊃and␈α⊃we␈α⊃must␈α⊃worry␈α⊃about␈α⊃input/output␈α⊃mechanisms␈α⊃so␈α⊃that␈α⊃the␈α⊃language␈α⊃may
␈↓ ↓H␈↓communicate with the external world.

␈↓ ↓H␈↓The␈α
present␈α
chapter␈α
will␈α
develop␈α
a␈α
picture␈α
of␈α
the␈α
␈↓↓static␈↓␈α
structure␈α
of␈α
an␈α
implementation,␈α
or␈α∞to␈α
be
␈↓ ↓H␈↓more␈α
graphic,␈αthis␈α
chapter␈α
describes␈αthe␈α
memory␈α
of␈αa␈α
LISP␈α
machine.␈α The␈α
next␈α
chapter␈αdiscusses
␈↓ ↓H␈↓the␈α∂␈↓↓dynamic␈α∂structure␈↓␈α∂of␈α∂LISP;␈α∂that␈α⊂is,␈α∂the␈α∂control␈α∂structures␈α∂necessary␈α∂to␈α⊂evaluate␈α∂expressions
␈↓ ↓H␈↓involving recursive functions and other LISP constructs.

␈↓ ↓H␈↓Throughout␈αthese␈αdiscussions␈α
we␈αwill␈αremain␈αas␈α
abstract␈αas␈αpossible␈α
without␈αlosing␈αtoo␈αmuch␈α
detail.
␈↓ ↓H␈↓We␈α∃will␈α∀describe␈α∃the␈α∀"logical"␈α∃structure␈α∃of␈α∀a␈α∃LISP␈α∀machine,␈α∃even␈α∀though␈α∃a␈α∃more␈α∀efficient
␈↓ ↓H␈↓implementation␈α
might␈αmap␈α
differing␈α
logical␈αstructures␈α
onto␈α
the␈αsame␈α
physical␈α
structures␈αby␈α
utilizing
␈↓ ↓H␈↓machine-dependent techniques.
␈↓ ↓H␈↓␈↓↓182  static structure␈↓ 45.2␈↓


␈↓ ↓H␈↓␈↓ ∧\␈↓↓5.2  Representation of S-expressions␈↓


␈↓ ↓H␈↓We previously have expressed the dotted pair ␈↓α(A . (B . C))␈↓ as:
␈↓"␈↓ ↓H␈↓∂                            /\
␈↓"␈↓ ↓H␈↓∂                           /  \
␈↓"␈↓ ↓H␈↓∂                          /   /\
␈↓"␈↓ ↓H␈↓∂                         /   /  \
␈↓"␈↓ ↓H␈↓∂                        ␈↓αA␈↓∂   ␈↓αB␈↓∂   ␈↓αC␈↓∂

␈↓ ↓H␈↓or occasionally (on page 13) as:
␈↓"␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
                  ααααα→~ # ~ #αβααααααααααα→~ # ~ # ~
␈↓"␈↓ ↓H␈↓
                        %αβα∀ααα$            %αβα∀αβα$
␈↓"␈↓ ↓H␈↓
                          ↓                    ↓   ↓
␈↓"␈↓ ↓H␈↓
                          ␈↓αA␈↓
                    ␈↓αB␈↓
   ␈↓αC␈↓



␈↓ ↓H␈↓This␈α∞second␈α∞style␈α∞of␈α
graphical␈α∞representation␈α∞has␈α∞a␈α∞direct␈α
representation␈α∞in␈α∞the␈α∞storage␈α∞layout␈α
of
␈↓ ↓H␈↓our␈αmachine.␈α Each␈α
"double-box"␈αwill␈αbe␈αrepresented␈α
by␈αa␈αmachine␈αlocation␈α
and␈αeach␈αarrow␈αwill␈α
be
␈↓ ↓H␈↓represented␈αas␈αa␈α␈↓↓pointer␈↓␈αto␈αanother␈αmachine␈αlocation.␈α Notice␈αthat␈αeach␈αbox␈αcontains␈αtwo␈αpointers;
␈↓ ↓H␈↓therefore␈αeach␈αcorresponding␈αmachine␈αlocation,␈α␈↓
loc␈↓,␈αwill␈αbe␈αinterpreted␈αas␈αcontaining␈αtwo␈αmachine
␈↓ ↓H␈↓addresses␈↓π 92␈↓.␈α∩The␈α∩left-hand␈α∪address␈α∩will␈α∩represent␈α∩the␈α∪␈↓αcar␈↓-branch;␈α∩the␈α∩right-hand␈α∪address␈α∩will
␈↓ ↓H␈↓represent the ␈↓αcdr␈↓-branch:

␈↓"␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"␈↓ ↓H␈↓
                  loc   ~ car ~ cdr ~
␈↓"␈↓ ↓H␈↓
                        %ααααα∀ααααα$

␈↓ ↓H␈↓The␈α
pointers␈αwill␈α
either␈αreference␈α
atoms␈αor␈α
point␈αto␈α
other␈αtwo-pointer␈α
boxes.␈α Literal␈α
atoms␈α-- like
␈↓ ↓H␈↓␈↓αA, B, C␈↓ --␈αwill␈αalso␈αbe␈αrepresented␈αin␈αmachine␈αlocations,␈αonly␈αhere␈αthe␈αcontents␈αof␈αeach␈αlocation␈αwill
␈↓ ↓H␈↓be␈α
an␈α
encoding␈α
of␈α
the␈α
name␈αof␈α
the␈α
atom.␈α
 Obviously␈α
the␈α
contents␈αof␈α
such␈α
a␈α
location␈α
must␈α
not␈αbe
␈↓ ↓H␈↓interpreted as pointers.

␈↓"␈↓ ↓H␈↓
                        ⊂αααααααααααααααααααααα⊃
␈↓"␈↓ ↓H␈↓
                  loc   ~ rep. of literal atom ~
␈↓"␈↓ ↓H␈↓
                        %αααααααααααααααααααααα$

␈↓ ↓H␈↓To␈α⊂help␈α⊂keep␈α⊂track␈α⊃of␈α⊂the␈α⊂different␈α⊂uses␈α⊂of␈α⊃machine␈α⊂locations␈α⊂we␈α⊂will␈α⊂partition␈α⊃our␈α⊂machine's
␈↓ ↓H␈↓memory␈α⊂into␈α∂two␈α⊂disjoint␈α∂spaces:␈α⊂␈↓↓pointer␈α∂space␈↓,␈α⊂which␈α∂will␈α⊂contain␈α∂two-pointer␈α⊂cells;␈α⊂and␈α∂␈↓↓atom
␈↓ ↓H␈↓↓space␈↓,␈α⊃which␈α⊂will␈α⊃contain␈α⊃information␈α⊂like␈α⊃atoms␈α⊂which␈α⊃should␈α⊃not␈α⊂be␈α⊃interpreted␈α⊃as␈α⊂pointers.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 92␈↓␈α
An␈αactual␈α
hardware␈αmachine␈α
may␈αnot␈α
be␈αof␈α
sufficient␈αword-size␈α
to␈αaccomodate␈α
two␈αaddresses;␈α
in
␈↓ ↓H␈↓this␈αcase,␈αseveral␈αreal␈αwords␈αmay␈αbe␈αneeded␈αto␈αrepresent␈αone␈αLISP␈αword.␈αFor␈αexample,␈αthe␈αPDP-11
␈↓ ↓H␈↓(16 bit words)␈α⊂implementations␈α⊃typically␈α⊂use␈α⊂two␈α⊃machine␈α⊂words␈α⊂([Har 75]),␈α⊃and␈α⊂micro-processor
␈↓ ↓H␈↓versions␈α⊂(8 bit words)␈α⊂may␈α⊃use␈α⊂four␈α⊂words␈α⊃([Pag 76]).␈α⊂In␈α⊂Section ␈α⊃we␈α⊂discuss␈α⊂a␈α⊃special␈α⊂compact
␈↓ ↓H␈↓representation of LISP cells.
␈↓ ↓H␈↓␈↓↓5.2␈↓ πJRepresentation of S-expressions     183␈↓


␈↓ ↓H␈↓Thus␈α⊃if␈α⊃the␈α⊃first␈α⊃box␈α⊃in␈α⊃our␈α⊃example␈α⊃were␈α⊃represented␈α⊃by␈α⊃location␈α⊃␈↓
100␈↓␈α⊃and␈α⊃the␈α⊃second␈α⊂were
␈↓ ↓H␈↓represented␈α
by␈α
location␈α
␈↓
405␈↓,␈αand␈α
the␈α
atoms␈α
␈↓αA␈↓,␈α␈↓αB␈↓,␈α
and␈α
␈↓αC␈↓␈α
were␈αrepresented␈α
by␈α
the␈α
numbers␈α
␈↓
40␈↓,␈α␈↓
41␈↓,
␈↓ ↓H␈↓and␈α⊂␈↓
42␈↓,␈α⊂and␈α⊂were␈α∂to␈α⊂be␈α⊂found␈α⊂in␈α∂locations␈α⊂␈↓
710␈↓,␈α⊂␈↓
762␈↓,␈α⊂and␈α∂␈↓
711␈↓,␈α⊂respectively,␈α⊂then␈α⊂the␈α∂following
␈↓ ↓H␈↓picture beginning at location ␈↓
100␈↓ could represent the dotted pair ␈↓α(A . (B . C))␈↓.

␈↓"␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"␈↓ ↓H␈↓
                  100   ~ 710 ~ 405 ~
␈↓"␈↓ ↓H␈↓
                        %ααααα∀ααααα$
␈↓"␈↓ ↓H␈↓
                           ...
␈↓"␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"␈↓ ↓H␈↓
                  405   ~ 762 ~ 711 ~
␈↓"␈↓ ↓H␈↓
                        %ααααα∀ααααα$
␈↓"␈↓ ↓H␈↓
                           ...
␈↓"␈↓ ↓H␈↓
                        ⊂ααααααααααα⊃
␈↓"␈↓ ↓H␈↓
                  710   ~        40 ~
␈↓"␈↓ ↓H␈↓
                        εαααααααααααλ
␈↓"␈↓ ↓H␈↓
                  711   ~        42 ~
␈↓"␈↓ ↓H␈↓
                        %ααααααααααα$
␈↓"␈↓ ↓H␈↓
                           ...
␈↓"␈↓ ↓H␈↓
                        ⊂ααααααααααα⊃
␈↓"␈↓ ↓H␈↓
                  762   ~        41 ~
␈↓"␈↓ ↓H␈↓
                        %ααααααααααα$


␈↓ ↓H␈↓Thus␈α∞the␈α
left␈α∞half␈α
of␈α∞location␈α
␈↓
100␈↓␈α∞points␈α
to␈α∞the␈α
representation␈α∞of␈α
the␈α∞atom␈α
␈↓αA␈↓␈α∞and␈α
the␈α∞right␈α
half
␈↓ ↓H␈↓points␈α
to␈α
the␈α
representation␈α
of␈αthe␈α
dotted␈α
pair␈α
␈↓α(B . C)␈↓.␈α
 Notice␈αtoo␈α
that␈α
given␈α
the␈α
entry␈α
point␈αinto
␈↓ ↓H␈↓the␈αrepresentation␈α--location␈α␈↓
100␈↓␈αin␈αthe␈α
example--␈αwe␈αcan␈αunambiguously␈αdiscover␈αthe␈αS-expr␈α
being
␈↓ ↓H␈↓represented.

␈↓ ↓H␈↓This␈α∪representation␈α∪of␈α∪Symbolic␈α∪expressions␈α∪is␈α∪a␈α∩special␈α∪case␈α∪of␈α∪a␈α∪scheme␈α∪called␈α∪␈↓↓linked␈α∩list
␈↓ ↓H␈↓↓structure␈↓.␈α
The␈α
term␈α
"linked"␈αrefers␈α
to␈α
the␈α
fact␈α
that␈αto␈α
find␈α
succeeding␈α
elements␈α
in␈αthe␈α
representation
␈↓ ↓H␈↓we␈αmust␈αfollow␈αthe␈αexplicit␈αpointers␈αas␈αopposed␈αto,␈αsay,␈αmerely␈αincrementing␈αan␈αarray␈αpointer.␈α The
␈↓ ↓H␈↓phrase␈α
"list␈α
structure"␈α
describes␈α
an␈α∞arbitrary␈α
interconnection␈α
of␈α
these␈α
two-pointer␈α∞nodes,␈α
including
␈↓ ↓H␈↓self-referential␈α
structures.␈α
We␈α
will␈α
discuss␈α
such␈αgeneral␈α
structures␈α
later;␈α
for␈α
the␈α
moment␈α
we␈αrestrict
␈↓ ↓H␈↓such constructions to LISP trees: no intersecting branches.

␈↓ ↓H␈↓The␈αparticular␈αbrand␈αof␈α
linked␈αlist␈αstructure␈αwhich␈αwe␈α
have␈αdemonstrated␈αis␈αcalled␈α
␈↓↓singly␈αlinked␈↓.
␈↓ ↓H␈↓The␈αadjective␈α"singly"␈αmeans␈αthat␈αonly␈α␈↓↓one␈↓␈αpointer␈αis␈αstored␈αas␈αthe␈αrepresentation␈αof␈αthe␈α
arrow,␈α␈↓
→␈↓.
␈↓ ↓H␈↓In␈α
the␈α
case␈α
of␈α
LISP,␈α
the␈α
terminology␈α
means␈αthat␈α
the␈α
representation␈α
of␈α
each␈α
pointer␈α
only␈α
tells␈αus␈α
how
␈↓ ↓H␈↓to␈α
find␈α
succeeding␈α∞elements␈α
in␈α
the␈α
structure.␈α∞For␈α
example,␈α
if␈α
we␈α∞were␈α
looking␈α
at␈α
location␈α∞␈↓
405␈↓␈α
the
␈↓ ↓H␈↓representation␈α∞tells␈α
us␈α∞how␈α∞to␈α
find␈α∞the␈α
␈↓αcar␈↓␈α∞or␈α∞␈↓αcdr␈↓;␈α
they're␈α∞at␈α
␈↓
762␈↓␈α∞and␈α∞␈↓
711␈↓␈α
respectively.␈α∞But␈α∞if␈α
we
␈↓ ↓H␈↓wanted␈α∪to␈α∪find␈α∪the␈α∩␈↓↓predecessor␈↓␈α∪of␈α∪␈↓
405␈↓␈α∪in␈α∪this␈α∩representation␈α∪it␈α∪would␈α∪require␈α∪some␈α∩further
␈↓ ↓H␈↓calculation.␈α We␈αwould␈α
have␈αto␈αstart␈αat␈α
the␈αbeginning␈αof␈αthe␈α
S-expr␈αrepresentation␈αand␈αlook␈α
for␈αa
␈↓ ↓H␈↓location␈α⊃such␈α∩that␈α⊃its␈α∩␈↓αcar␈↓␈α⊃or␈α∩␈↓αcdr␈↓␈α⊃is␈α∩the␈α⊃desired␈α∩cell.␈α⊃ If␈α∩our␈α⊃use␈α∩of␈α⊃S-exprs␈α∩required␈α⊃frequent
␈↓ ↓H␈↓discovery␈αof␈αsuch␈α
predecessors␈αthen␈αwe␈α
might␈αconsider␈αan␈α
even␈αmore␈αcomplex␈αlinked␈α
representation
␈↓ ↓H␈↓which␈α
would␈α
also␈α
contain␈α
information␈α
about␈α
the␈α
predecessor␈α
of␈α
each␈α
node,␈α
essentially␈αrepresenting␈α
␈↓
→␈↓
␈↓ ↓H␈↓as ␈↓
↔␈↓.
␈↓ ↓H␈↓%2184  static structure␈↓ _5.2%*



␈↓ ↓H␈↓For example:
␈↓"␈↓ ↓H␈↓
                        ⊂αααπααα⊃            ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
                   α←→α→~ # ~ #←βααα←→αααααα→~ # ~ # ~
␈↓"␈↓ ↓H␈↓
                        %αβα∀ααα$            %αβα∀αβα$
␈↓"␈↓ ↓H␈↓
                          ↑                    ↑   ↑
␈↓"␈↓ ↓H␈↓
                          ↓                    ↓   ↓
␈↓"␈↓ ↓H␈↓
                          ␈↓αA␈↓
                    ␈↓αB␈↓
   ␈↓αC␈↓



␈↓ ↓H␈↓One␈αsuch␈αrepresentation␈αis␈αcalled␈α␈↓↓doubly-linked␈αlist␈αstructure␈↓.␈αIn␈αthis␈αrepresentation␈αof␈αLISP␈αtrees
␈↓ ↓H␈↓we could store ␈↓↓three␈↓ pieces of information with each non-terminal node:

␈↓"␈↓ ↓H␈↓
                        ⊂ααααααααααααα⊃
␈↓"␈↓ ↓H␈↓
                        ~ predecessor ~
␈↓"␈↓ ↓H␈↓
                  loc   εααααααπααααααλ
␈↓"␈↓ ↓H␈↓
                        ~  car ~  cdr ~
␈↓"␈↓ ↓H␈↓
                        %αααααα∀αααααα$

␈↓ ↓H␈↓Note␈α⊂that␈α⊂LISP␈α⊃trees␈α⊂always␈α⊂␈↓↓do␈↓␈α⊃have␈α⊂unique␈α⊂predecessors.␈α⊂In␈α⊃the␈α⊂case␈α⊂of␈α⊃list-structure,␈α⊂unique
␈↓ ↓H␈↓predecessors␈αdo␈αnot␈αalways␈αexist.␈αCompromises␈αexist␈αin␈αsome␈αapplications:␈αsome␈αdata␈αstructures␈αcan
␈↓ ↓H␈↓be␈αdoubly-linked,␈αallowing␈α
fast␈αtraversal␈αbut␈α
requiring␈αmore␈αspace;␈α
while␈αother␈αdata␈α
structures␈αare
␈↓ ↓H␈↓singly␈α∩linked,␈α∩requiring␈α∩less␈α∩space,␈α∩but␈α∩requiring␈α∩more␈α∩time␈α∩to␈α∩traverse␈α∩([Gua xx]);␈α∩still␈α⊃other
␈↓ ↓H␈↓structures␈α∂may␈α∂have␈α∞more␈α∂compact␈α∂representation␈α∞as␈α∂arrays␈α∂or␈α∞numbers.␈α∂ For␈α∂example,␈α∂a␈α∞typical
␈↓ ↓H␈↓representation␈α⊂of␈α⊂a␈α⊂vector,␈α⊂or␈α⊂sequence␈α⊂of␈α⊂fixed␈α⊂length,␈α⊂is␈α⊂to␈α⊂store␈α⊂the␈α⊂elements␈α⊃sequentially␈α⊂in
␈↓ ↓H␈↓memory␈↓π 93␈↓.␈α
 Since␈α
each␈α
element␈α
in␈α
this␈α
structure␈αhas␈α
at␈α
most␈α
one␈α
successor␈α
we␈α
can␈α
use␈αthe␈α
sequential
␈↓ ↓H␈↓addresses␈α
as␈α
implicit␈α
pointers␈αto␈α
retrieve␈α
successive␈α
elements.␈αA␈α
general␈α
S-expr␈α
has␈α
two␈αsuccessors;
␈↓ ↓H␈↓thus␈α∞the␈α∞implied␈α∞linear␈α
addressing␈α∞scheme␈α∞of␈α∞most␈α∞machine␈α
memories␈α∞is␈α∞insufficient␈α∞as␈α∞it␈α
stands;
␈↓ ↓H␈↓LISP␈α∂uses␈α∂linked␈α∂allocation.␈α∂ Again␈α∂there␈α∂are␈α∂compromises.␈α∂ For␈α∂example␈α∂the␈α∂following␈α∂memory
␈↓ ↓H␈↓representation␈α∞is␈α∞valid␈α∞for␈α∞LISP␈α∞trees:␈α∞for␈α∞any␈α∞location␈α∞␈↓αn␈↓,␈α∞find␈α∞its␈α∞successors␈α∞at␈α∞locations␈α∂␈↓α2n␈↓␈α∞and
␈↓ ↓H␈↓␈↓α2n+1␈↓;␈αnote␈αthat␈αthe␈αpredecessor␈αof␈αany␈α
cell␈αis␈αunique.␈α Each␈αlocation␈αmust␈αcontain␈αan␈α
indication␈αof
␈↓ ↓H␈↓whether or not it is an atom.  The remaining contents of a location is available for data[Ber 71].

␈↓ ↓H␈↓We␈αwill␈αfrequently␈αwish␈αto␈αrefer␈αto␈αseveral␈αdifferent␈αS-exprs␈αsimultaneously;␈αfor␈αexample,␈αwhen␈αwe
␈↓ ↓H␈↓are␈α_talking␈α_about␈α↔the␈α_implementation␈α_of␈α↔the␈α_function␈α_␈↓αcons␈↓␈α↔we␈α_will␈α_be␈α_manipulating␈α↔the
␈↓ ↓H␈↓representations␈αof␈αtwo␈αS-exprs.␈αSimilarly␈αwe␈αwill␈αwant␈αto␈αrefer␈αto␈αseveral␈αpieces␈αof␈αa␈αsingle␈αcomplex
␈↓ ↓H␈↓S-expr;␈α∞for␈α∞example␈α
we␈α∞might␈α∞wish␈α
to␈α∞"put␈α∞a␈α
finger"␈α∞at␈α∞a␈α
specific␈α∞point␈α∞in␈α
a␈α∞structure␈α∞and␈α
then,
␈↓ ↓H␈↓depending␈αon␈αthe␈αresult␈αof␈αa␈αcomputation␈αon␈αsome␈αsub-part,␈αmove␈αthe␈α"finger"␈αeither␈αleft␈αor␈αright.
␈↓ ↓H␈↓To␈α∩facilitate␈α∩such␈α∩discussions␈α∩we␈α∩will␈α∩assume␈α∩the␈α∩existence␈α∩of␈α∩a␈α∩set␈α∩of␈α∩pointer␈α∪registers:␈α∩␈↓
Pt␈↓β1␈↓,
␈↓ ↓H␈↓␈↓
Pt␈↓β2␈↓, ..., ␈↓
Pt␈↓βn␈↓.␈α
 Thus,␈α
using␈α
the␈α
above␈α
example,␈α
the␈α
following␈α
represents␈α
␈↓
Pt␈↓β1␈↓␈α
pointing␈α
at␈α
␈↓α(B . C)␈↓␈αand
␈↓ ↓H␈↓␈↓
Pt␈↓β2␈↓ pointing at the atom ␈↓αA␈↓:

␈↓"␈↓ ↓H␈↓
           Pt␈↓β1␈↓
                                   Pt␈↓β2␈↓

␈↓"␈↓ ↓H␈↓
        ⊂αααααααα⊃                      ⊂αααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~   405  ~                      ~   710  ~
␈↓"␈↓ ↓H␈↓
        %αααααααα$                      %αααααααα$

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 93␈↓ We will discuss these more detailed representations in Chapter .
␈↓ ↓H␈↓␈↓↓5.2␈↓ πJRepresentation of S-expressions     185␈↓


␈↓ ↓H␈↓Implicit␈αin␈αour␈α
representation␈αis␈αthe␈α
assurance␈αthat␈αwe␈αcan␈α
differentiate␈αbetween␈αlocations␈α
in␈αatom
␈↓ ↓H␈↓space␈αand␈αlocations␈αin␈αpointer␈αspace.␈αFor␈αexample,␈αassume␈αeach␈αof␈αour␈αlocations␈αcan␈αhold␈αsix␈αdigits
␈↓ ↓H␈↓and␈α∂assume␈α⊂we␈α∂will␈α⊂store␈α∂a␈α∂numeric␈α⊂atom␈α∂as␈α⊂its␈α∂corresponding␈α∂number.␈α⊂Then␈α∂the␈α⊂atom␈α∂␈↓α762711␈↓
␈↓ ↓H␈↓would be stored as:
␈↓"␈↓ ↓H␈↓
                        ⊂αααααααα⊃
␈↓"␈↓ ↓H␈↓
                        ~ 762711 ~
␈↓"␈↓ ↓H␈↓
                        %αααααααα$

␈↓ ↓H␈↓Since␈α∞this␈α∂is␈α∞exactly␈α∞the␈α∂contents␈α∞of␈α∂location␈α∞␈↓
405␈↓␈↓π 94␈↓␈α∞some␈α∂confusion␈α∞is␈α∞possible:␈α∂is␈α∞the␈α∂contents␈α∞a
␈↓ ↓H␈↓number␈αor␈αis␈α
it␈αtwo␈αpointers?␈α
A␈αtypical␈αtrick␈αis␈α
to␈αpartition␈αmemory␈α
such␈αthat␈αparticular␈αportions␈α
of
␈↓ ↓H␈↓the␈α∂address␈α∂space␈α∂correspond␈α∂to␈α∂each␈α∂of␈α∂the␈α∂logical␈α∂spaces:␈α∂atom␈α∂space␈α∂or␈α∂pointer␈α∂space.␈α∂In␈α∂our
␈↓ ↓H␈↓example␈αwe␈αcould␈αassume␈αthat␈αaddresses␈αbelow␈α␈↓
700␈↓␈αare␈αlocations␈αfor␈αpointers,␈αwhile␈αaddresses␈α␈↓
700␈↓
␈↓ ↓H␈↓and␈α⊂above␈α⊂contain␈α⊃atoms.␈α⊂Thus␈α⊂the␈α⊃representation␈α⊂of␈α⊂␈↓α762711␈↓␈α⊂would␈α⊃appear␈α⊂in␈α⊂a␈α⊃location␈α⊂with
␈↓ ↓H␈↓address ␈↓
700␈↓ or greater.

␈↓ ↓H␈↓Though␈α∪our␈α∪memory␈α∪system␈α∪is␈α∪not␈α∪completed␈α∪yet,␈α∪we␈α∪␈↓↓do␈↓␈α∪have␈α∪enough␈α∪structure␈α∪to␈α∀begin␈α∪a
␈↓ ↓H␈↓discussion of the implementation of some of the primitive LISP operations.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓What problems do you foresee in using the double-linking scheme for representing LISP's S-exprs?



␈↓ ↓H␈↓␈↓ ∧J␈↓↓5.3  Representation of LISP primitives␈↓


␈↓ ↓H␈↓Now␈αthat␈αwe␈αhave␈αsome␈αof␈αthe␈αrepresentational␈αproblems␈αfor␈αSymbolic␈αexpressions␈αreasonably␈αwell
␈↓ ↓H␈↓in␈α
hand␈α
we␈α
will␈α
look␈αat␈α
the␈α
implementation␈α
of␈α
the␈α
LISP␈αprimitives.␈α
We␈α
will␈α
examine␈α
␈↓αcar,␈α
cdr,␈αeq,␈↓
␈↓ ↓H␈↓and ␈↓αatom␈↓ in this section, leaving ␈↓αcons␈↓ for later.

␈↓ ↓H␈↓We␈α
need␈α
to␈α
understand␈α
how␈α
these␈α
primitives␈α
are␈αto␈α
obtain␈α
their␈α
parameters␈α
and␈α
how␈α
they␈α
are␈αto
␈↓ ↓H␈↓return␈α∞their␈α∞values.␈α∞ Recall␈α∞our␈α∂discussion␈α∞of␈α∞environments␈α∞and␈α∞destinations␈α∞in␈α∂Section 4.17.␈α∞ An
␈↓ ↓H␈↓environment␈α
chain␈α
was␈α
constructed␈α
by␈α
linking␈αdestination␈α
blocks␈α
whose␈α
value␈α
slots␈α
have␈αbeen␈α
filled.
␈↓ ↓H␈↓A␈αdest-block␈αwas␈αcreated␈αwhen␈αwe␈αrecognized␈αa␈αfunction␈αapplication␈↓π 95␈↓.␈α The␈αname␈αcomponents␈αof

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 94␈↓ The vertical bar doesn't appear in the machine's memory.

␈↓ ↓H␈↓␈↓π 95␈↓␈α∪If␈α∩the␈α∪application␈α∪supplied␈α∩an␈α∪incorrect␈α∪number␈α∩of␈α∪arguments␈α∪no␈α∩dest-block␈α∪is␈α∪built;␈α∩the
␈↓ ↓H␈↓debugging␈αroutine␈α
is␈αcalled.␈αSeveral␈α
existing␈αimplementations␈αsupply␈α
missing␈αarguments␈α
with␈α␈↓αNIL␈↓
␈↓ ↓H␈↓or␈αevaluate␈αand␈αdiscard␈αextra␈αarguments.␈αThis␈αtreatment␈αof␈αimproper␈αcalling␈αsequences␈αignores␈αone
␈↓ ↓H␈↓of the most common sources of bugs in LISP programs ([Mot 76]).
␈↓ ↓H␈↓␈↓↓186  static structure␈↓ 45.3␈↓


␈↓ ↓H␈↓a␈α
block␈α
are␈αeither␈α
the␈α
λ-variables␈αin␈α
the␈α
case␈αof␈α
a␈α
λ-application␈αor␈α
are␈α
system-generated␈α
names␈αin
␈↓ ↓H␈↓the␈α⊃case␈α⊃of␈α⊃primitive␈α∩application;␈α⊃the␈α⊃value-slots␈α⊃of␈α∩a␈α⊃dest-block␈α⊃received␈α⊃the␈α∩evaluated␈α⊃actual
␈↓ ↓H␈↓parameters.␈α
 When␈α
a␈α
dest-block␈α
was␈α
filled,␈α
it␈α
was␈α
chained␈α
onto␈α
the␈α
front␈α
of␈α
the␈α
environment␈αand␈α
we
␈↓ ↓H␈↓were␈α
ready␈αto␈α
call␈αthe␈α
function.␈α
Thus␈αthe␈α
first␈αblock␈α
on␈α
the␈αenvironment␈α
chain␈αwas␈α
the␈αlocal␈α
symbol
␈↓ ↓H␈↓table.␈α The␈αfunction␈αwas␈αexpected␈αto␈αreturn␈αits␈αvalue␈αto␈αa␈αdesignated␈αdest-block,␈αand␈αthen␈αreturn␈αto
␈↓ ↓H␈↓the␈α∪interrupted␈α∪computation.␈α∩ So,␈α∪on␈α∪entrance␈α∩to␈α∪a␈α∪primitive␈α∩we␈α∪have␈α∪access␈α∩to␈α∪at␈α∪least␈α∩two
␈↓ ↓H␈↓structures: a destination block and the environment chain.

␈↓"␈↓ ↓H␈↓
    ␈↓αdest␈↓
                           ␈↓αenv␈↓

␈↓"␈↓ ↓H␈↓
    ~                              ~
␈↓"␈↓ ↓H␈↓
    ~   ⊂ααααααα⊃                  ~    ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
    %α→ ~     #αβα⊃                %αα→ ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                           # # #
␈↓"␈↓ ↓H␈↓
        ~   ~   ~←$                     ~   ~   ~
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       εαααβαααλ
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       ~   ~   ~
␈↓"␈↓ ↓H␈↓
          # # #                         %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
        εαααβαααλ
␈↓"␈↓ ↓H␈↓
        ~   ~   ~
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$

␈↓ ↓H␈↓Here is how ␈↓αcar␈↓ uses these structures:

␈↓ ↓H␈↓␈↓ αhLet␈α␈↓αval␈↓␈αbe␈αthe␈αvalue-part␈αof␈αthe␈αfirst␈αentry␈αin␈αthe␈αlocal␈αtable.␈αIf␈α␈↓αval␈↓␈αis␈αan␈αatom␈αthen␈α␈↓αcar␈↓
␈↓ ↓H␈↓␈↓ αhis␈αundefined;␈αthe␈αimplementation␈αshould␈αsend␈αa␈αmessage␈αto␈αthe␈αdebugging␈αpackage␈α
(see
␈↓ ↓H␈↓␈↓ αhSection ).␈α∂ If␈α∂␈↓αval␈↓␈α∂is␈α∂␈↓↓not␈↓␈α∂atomic,␈α∂it␈α∂has␈α∂a␈α∂left-␈α∂and␈α∂right-hand␈α∂side.␈α∂ We␈α⊂should␈α∂send
␈↓ ↓H␈↓␈↓ αhthe ␈↓↓left␈↓-hand side of ␈↓αval␈↓ to the value part of the slot pointed to in the dest-block.
␈↓ ↓H␈↓%25.3␈↓ π∨Representation of LISP primitives     187%*



␈↓ ↓H␈↓For example:
␈↓"␈↓ ↓H␈↓
    ␈↓αdest␈↓
                            ␈↓αenv␈↓

␈↓"␈↓ ↓H␈↓
    ~                               ~
␈↓"␈↓ ↓H␈↓
    ~   ⊂ααααααα⊃                   ~   ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
    %→  ~     #αβα⊃                 %→  ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                         ~   ~102~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~←$                     %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
        εαααβαααλ
␈↓"␈↓ ↓H␈↓
        ~   ~   ~
␈↓"␈↓ ↓H␈↓
          # # #
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                102    ~204~ 22~
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$                       %ααα∀ααα$
␈↓"␈↓ ↓H␈↓↓␈↓ ε%Before

␈↓"␈↓ ↓H␈↓
        ⊂ααααααα⊃                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~     #αβα⊃                     ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                         ~   ~102~
␈↓"␈↓ ↓H␈↓
        ~   ~204~←$                     %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
        εαααβαααλ
␈↓"␈↓ ↓H␈↓
        ~   ~   ~
␈↓"␈↓ ↓H␈↓
          # # #
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                102    ~204~ 22~
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$                       %ααα∀ααα$
␈↓ ↓H␈↓↓␈↓ ε,After
␈↓ ↓H␈↓↓␈↓ ¬bExample for ␈↓αcar␈↓↓

␈↓ ↓H␈↓For␈αsuccessful␈α
completion␈α␈↓
␈↓αcar␈↓␈α
expects␈αits␈α
actual␈αparameter␈αto␈α
be␈αpointing␈α
to␈αa␈α
node␈αin␈αpointer␈α
space;
␈↓ ↓H␈↓otherwise␈α∞we␈α∞get␈α∞an␈α
error.␈α∞If␈α∞the␈α∞operation␈α
is␈α∞successful␈α∞then␈α∞the␈α
dest-slot␈α∞is␈α∞changed␈α∞to␈α∞point␈α
to
␈↓ ↓H␈↓whatever␈α⊃was␈α⊃pointed␈α⊃to␈α⊃by␈α⊃the␈α⊃left-hand␈α⊃side␈α⊃of␈α⊃the␈α⊃selected␈α⊃cell.␈α⊃ The␈α⊃description␈α⊃of␈α∩␈↓αcdr␈↓␈α⊃is
␈↓ ↓H␈↓sufficiently similar that we leave it to the reader.
␈↓ ↓H␈↓%2188  static structure␈↓ _5.3%*



␈↓ ↓H␈↓On␈αpage 185␈αwe␈αdescribed␈αthe␈αinternal␈α
structure␈αof␈αLISP␈αatoms.␈αUsing␈αthat␈αrepresentation␈α
we␈αcan
␈↓ ↓H␈↓give a simple implementation for the predicate ␈↓αatom␈↓:

␈↓ ↓H␈↓␈↓ αhDoes␈αthe␈αactual␈αparameter␈αpoint␈αinto␈αthat␈αarea␈αreserved␈αfor␈αatom␈αnames?␈αIf␈αso,␈αsend␈αa
␈↓ ↓H␈↓␈↓ αhrepresentation of truth as value, otherwise send a representation of false.

␈↓"␈↓ ↓H␈↓
   ␈↓αdest␈↓
                             ␈↓αenv␈↓

␈↓"␈↓ ↓H␈↓
    ~                              ~
␈↓"␈↓ ↓H␈↓
    ~   ⊂ααααααα⊃                  ~    ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
    %→  ~     #αβα⊃                %→   ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                         ~   ~710~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~←$                     %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
        εαααβαααλ
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
          # # #                  710    ~    "A"~
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       %ααααααα$
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$                714    ~    "T"~
␈↓"␈↓ ↓H␈↓
                                        %ααααααα$
␈↓"␈↓ ↓H␈↓
␈↓We are writing ␈↓
"A"␈↓ instead of the numeric encoding. Thus ␈↓
"A"␈↓ is really ␈↓
40␈↓.␈↓

␈↓"␈↓ ↓H␈↓↓␈↓ ε%Before

␈↓"␈↓ ↓H␈↓
        ⊂ααααααα⊃                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~     #αβα⊃                     ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                         ~   ~710~
␈↓"␈↓ ↓H␈↓
        ~   ~714~←$                     %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
        εαααβαααλ
␈↓"␈↓ ↓H␈↓
        ~   ~   ~
␈↓"␈↓ ↓H␈↓
          # # #
␈↓"␈↓ ↓H␈↓
        εαααβαααλ
␈↓"␈↓ ↓H␈↓
        ~   ~   ~
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$
␈↓ ↓H␈↓↓␈↓ ε,After
␈↓ ↓H␈↓↓␈↓ ¬XExample for ␈↓αatom␈↓↓

␈↓ ↓H␈↓Notice␈αthat␈αwe␈αdid␈αnot␈αneed␈αto␈αexamine␈αthe␈αcontents␈αof␈αlocation␈α␈↓
710␈↓;␈αit␈αwas␈αsufficient␈αto␈αknow␈αthat
␈↓ ↓H␈↓the␈α∞location␈α
was␈α∞between␈α
predetermined␈α∞bounds.␈α∞ If␈α
the␈α∞actual␈α
parameter␈α∞was␈α
not␈α∞pointing␈α∞at␈α
an
␈↓ ↓H␈↓atom we would have returned a pointer to a location containing ␈↓
"NIL"␈↓.
␈↓ ↓H␈↓␈↓↓5.3␈↓ π%Representation of LISP primitives     189␈↓



␈↓ ↓H␈↓Finally we describe an implementation of ␈↓αeq␈↓.

␈↓ ↓H␈↓␈↓ αhDo␈αboth␈αacutal␈αparameters␈αboth␈αpoint␈αinto␈αatom␈αspace?␈α If␈αnot␈αthe␈αresult␈αis␈αundefined.
␈↓ ↓H␈↓␈↓ αhIf␈α∩they␈α∩␈↓↓do␈↓␈α∩then␈α∪do␈α∩they␈α∩reference␈α∩the␈α∩same␈α∪atom?␈α∩ We␈α∩can␈α∩determine␈α∪this␈α∩latter
␈↓ ↓H␈↓␈↓ αhcondition␈αin␈αtwo␈αways:␈αfirst,␈αthey␈αmight␈αpoint␈αto␈αtwo␈αdifferent␈αlocations␈αin␈α
atom␈αspace;
␈↓ ↓H␈↓␈↓ αhwe␈α∞would␈α∂have␈α∞to␈α∂examine␈α∞the␈α∂contents␈α∞of␈α∞those␈α∂locations;␈α∞if␈α∂they␈α∞agreed␈α∂then␈α∞␈↓αatom␈↓
␈↓ ↓H␈↓␈↓ αhshould␈α∞return␈α∞a␈α∞representation␈α∞of␈α∞truth.␈α∞A␈α∞more␈α∞satisfactory␈α∞solution␈α∞is␈α∞to␈α∞store␈α∞each
␈↓ ↓H␈↓␈↓ αhatom␈α∂␈↓↓uniquely␈↓;␈α∂one␈α∂location␈α∂will␈α∂be␈α∂reserved␈α∂for␈α∂␈↓
"A"␈↓,␈α∂etc.␈α∂Now␈α∂all␈α∂␈↓αatom␈↓␈α∂need␈α⊂do␈α∂is
␈↓ ↓H␈↓␈↓ αhmake␈αsure␈α
that␈αboth␈αslots␈α
point␈αinto␈αatom␈α
space␈α␈↓↓and␈↓␈αpoint␈α
to␈αthe␈αsame␈α
location.␈αThus
␈↓ ↓H␈↓␈↓ αhno␈α⊂additional␈α⊂memory␈α⊃reference␈α⊂is␈α⊂required.␈α⊃From␈α⊂now␈α⊂on␈α⊃we␈α⊂will␈α⊂assume␈α⊃that␈α⊂all
␈↓ ↓H␈↓␈↓ αhatoms are indeed stored uniquely.

␈↓"␈↓ ↓H␈↓
   ␈↓αdest␈↓
                            ␈↓αenv␈↓

␈↓"␈↓ ↓H␈↓
    ~                              ~
␈↓"␈↓ ↓H␈↓
    ~   ⊂ααααααα⊃                  ~    ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
    %→  ~     #αβα⊃                %→   ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                         ~   ~710~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~←$                     εαααβαααλ
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       ~   ~710~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
          # # #                         ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                714    ~    "T"~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       %ααααααα$
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
                                 710    ~    "A"~
␈↓"␈↓ ↓H␈↓
                                        %ααααααα$
␈↓"␈↓ ↓H␈↓↓␈↓ ε%Before


␈↓"␈↓ ↓H␈↓
        ⊂ααααααα⊃                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~     #αβα⊃                     ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                         ~   ~710~
␈↓"␈↓ ↓H␈↓
        ~   ~714~←$                     εαααβαααλ
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       ~   ~710~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
          # # #                         ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                714    ~    "T"~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       %ααααααα$
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
                                 710    ~    "A"~
␈↓"␈↓ ↓H␈↓
                                        %ααααααα$
␈↓ ↓H␈↓↓␈↓ ε,After
␈↓ ↓H␈↓↓␈↓ ¬iExample for ␈↓αeq␈↓↓

␈↓ ↓H␈↓We␈α
still␈αhave␈α
a␈α
representational␈αproblem␈α
to␈α
resolve:␈αif␈α
we␈α
wish␈αto␈α
represent␈α
the␈αnumber␈α
␈↓α40␈↓␈α
as␈α␈↓
40␈↓
␈↓ ↓H␈↓then␈α∪we␈α∪have␈α∪a␈α∪conflict␈α∪with␈α∪our␈α∩proposed␈α∪representation␈α∪of␈α∪the␈α∪atom␈α∪␈↓αA␈↓␈α∪as␈α∪␈↓
40␈↓.␈α∩Section 5.5
␈↓ ↓H␈↓resolves this conflict.
␈↓ ↓H␈↓␈↓↓190  static structure␈↓ 45.3␈↓


␈↓ ↓H␈↓␈↓ ε␈↓↓AMBIT/G␈↓


␈↓ ↓H␈↓Before␈α
looking␈α
more␈α
deeply␈α
into␈αthe␈α
detailed␈α
nature␈α
of␈α
atoms,␈αwe␈α
should␈α
explore␈α
a␈α
possibility␈αfor
␈↓ ↓H␈↓describing␈αLISP's␈αprimitives.␈α We␈αhave␈αdescribed␈αthem␈α
by␈αexample,␈αwithout␈αmuch␈αloss␈αin␈αclarity.␈α
It
␈↓ ↓H␈↓would␈αbe␈αmore␈αpleasing␈αif␈αwe␈αcould␈αdescribe␈αeach␈αprimitive␈αin␈αmore␈αgeneral␈αterms.␈α Fortunately␈αwe
␈↓ ↓H␈↓can␈α∞give␈α∞less␈α∞machine-dependent␈α∞characterizations␈α∞using␈α∞a␈α∞micro-version␈α∞of␈α∞a␈α∞graphical␈α
language
␈↓ ↓H␈↓called AMBIT/G␈↓π 96␈↓.

␈↓ ↓H␈↓When␈α∩faced␈α⊃with␈α∩explaining␈α∩a␈α⊃complex␈α∩structure-manipulating␈α∩program␈α⊃to␈α∩someone,␈α∩we␈α⊃draw
␈↓ ↓H␈↓pictures.␈α⊂We␈α⊃do␈α⊂it␈α⊃in␈α⊂LISP␈α⊂to␈α⊃describe␈α⊂the␈α⊃data␈α⊂structures␈α⊂and␈α⊃in␈α⊂this␈α⊃section␈α⊂we␈α⊃have␈α⊂given
␈↓ ↓H␈↓graphical␈αdescriptions␈α
of␈αthe␈αLISP␈α
primitives␈αusing␈αexamples.␈α
AMBIT␈αis␈αan␈α
extension␈αof␈α
both␈αof
␈↓ ↓H␈↓these ideas; it is a graphical language for the description of both data and algorithms.

␈↓ ↓H␈↓The␈α∩basic␈α∩statements␈α∩of␈α∪the␈α∩language␈α∩are␈α∩␈↓↓pattern-match-and-replacement␈↓␈α∩rules.␈α∪ Patterns␈α∩are
␈↓ ↓H␈↓described␈αas␈αcombinations␈αof␈αshapes␈αand␈αsolid␈αarrows.␈α If␈αan␈αinstance␈αof␈αa␈αpattern␈αcan␈αbe␈αfound␈αin
␈↓ ↓H␈↓the␈α
current␈α
state␈α
of␈α
the␈α
computation,␈α
then␈α∞we␈α
will␈α
replace␈α
that␈α
instance␈α
with␈α
a␈α
new␈α∞pattern.␈α
 The
␈↓ ↓H␈↓only␈α
kind␈α
of␈αreplacement␈α
we␈α
will␈α
allow␈αis␈α
the␈α
␈↓↓swinging␈↓␈αof␈α
an␈α
arrow␈α
so␈αthat␈α
its␈α
head␈α
moves␈αfrom
␈↓ ↓H␈↓one␈αnode␈αto␈αanother.␈α
 Thus␈αthe␈αnew␈αpattern␈αdiffers␈α
from␈αthe␈αold␈αonly␈α
in␈αthe␈αpositioning␈αof␈αsome␈α
of
␈↓ ↓H␈↓the␈αarrows.␈α Where␈αthe␈αarrow␈αhead␈αstrikes␈αa␈αnode␈αis␈αimmaterial;␈αthe␈αorigin␈αof␈αthe␈αtail␈α␈↓↓is␈↓␈αimportant.
␈↓ ↓H␈↓Dashed␈α⊃arrows␈α⊂show␈α⊃replacements␈α⊂to␈α⊃be␈α⊃made␈α⊂if␈α⊃the␈α⊂pattern␈α⊃matches.␈α⊂ Portions␈α⊃of␈α⊃the␈α⊂shapes
␈↓ ↓H␈↓marked with "?" are "don't-care" conditions.

␈↓ ↓H␈↓For example, here's ␈↓αcar␈↓:
␈↓"␈↓ ↓H␈↓
    ␈↓αdest␈↓
                            ␈↓αenv␈↓

␈↓"␈↓ ↓H␈↓
    ~                              ~
␈↓"␈↓ ↓H␈↓
    ~   ⊂ααααααα⊃                  ~    ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
    %→  ~     #αβα⊃                %→   ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #                         ~   ~ # ~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~←$                     %ααα∀αβα$
␈↓"␈↓ ↓H␈↓
        ~   ~ # → - - - - →                   ~
␈↓"␈↓ ↓H␈↓
        εαααβαααλ         ↓                   ↓
␈↓"␈↓ ↓H␈↓
        ~   ~   ~                       ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
          # # #           |             ~ # ~ ? ~
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       %αβα∀ααα$
␈↓"␈↓ ↓H␈↓
        ~   ~   ~         ↓               ↓
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$                       ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
                          %→ - - → - - →~   ?   ~
␈↓"␈↓ ↓H␈↓
                                        %ααααααα$
␈↓ ↓H␈↓↓␈↓ ¬\algorithm for ␈↓αcar␈↓↓

␈↓ ↓H␈↓This␈α∞AMBIT␈α∞diagram␈α∞contains␈α∞equivalent␈α∞information␈α∂to␈α∞the␈α∞previous␈α∞␈↓↓example␈↓␈α∞of␈α∞␈↓αcar␈↓,␈α∂but␈α∞the
␈↓ ↓H␈↓extraneous␈α∞details␈α∞of␈α∂which␈α∞addresses␈α∞are␈α∂involved␈α∞have␈α∞been␈α∞stripped␈α∂away.␈α∞ We␈α∞will␈α∂use␈α∞such
␈↓ ↓H␈↓diagrams occasionally when they will contribute to clarity.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 96␈↓␈α↔An␈α↔ambit␈α↔is␈α↔half␈α↔aminal,␈α↔half␈α↔hobbit.␈α↔AMBIT/G␈α↔also␈α↔is␈α↔an␈α↔acronym␈α↔for␈α↔Algebraic
␈↓ ↓H␈↓Manipulation By Identity Transformation/Graphical.
␈↓ ↓H␈↓␈↓↓5.3␈↓ π%Representation of LISP primitives     191␈↓


␈↓ ↓H␈↓␈↓ ε↔␈↓↓Problem␈↓


␈↓ ↓H␈↓Give an AMBIT diagram for ␈↓αeq␈↓.



␈↓ ↓H␈↓␈↓ ∧[␈↓↓5.4  A few programming techniques␈↓


␈↓ ↓H␈↓There␈αare␈αa␈α
few␈αuseful␈αand␈αpractical␈α
LISP␈αprogramming␈αtechniques␈α
which␈αtake␈αadvantage␈αof␈α
some
␈↓ ↓H␈↓of␈α
the␈α
tricks␈αof␈α
implementation.␈α
 These␈αtricks␈α
are␈α
supported␈α
in␈αmost␈α
implementations␈α
and␈αare␈α
useful
␈↓ ↓H␈↓enough that they should be documented as programming language features.

␈↓ ↓H␈↓␈↓↓1.␈↓␈α⊃In␈α⊃most␈α⊃implementations␈α⊃of␈α⊃␈↓αeq␈↓␈α⊃the␈α⊃check␈α⊃for␈α⊃atom-ness␈α⊃is␈α⊃suppressed␈α⊃and␈α⊃a␈α⊃simple␈α⊃address
␈↓ ↓H␈↓␈↓ α_comparison␈α∞is␈α∂made.␈α∞Non-atomic␈α∞S-expressions␈α∂are␈α∞not␈α∞usually␈α∂stored␈α∞uniquely␈↓π 97␈↓;␈α∂Thus␈α∞in
␈↓ ↓H␈↓␈↓ α_most implementations

␈↓ ↓H␈↓␈↓ ∧\␈↓αeq[(A . B);(A . B)]␈↓ is usually false, but 

␈↓ ↓H␈↓␈↓ ¬β␈↓αeq[x;x] ␈↓is usually true for any ␈↓αx␈↓. 

␈↓ ↓H␈↓␈↓ α_Please␈αnote␈αthat␈α
we␈αare␈α␈↓↓not␈↓␈α
changing␈αthe␈αdefinition␈α
of␈α␈↓αeq␈↓.␈α␈↓αeq␈↓␈α
is␈αstill␈αundefined␈αfor␈α
non-atomic
␈↓ ↓H␈↓␈↓ α_arguments.  The preceding remarks deal only with the usual implementation of ␈↓αeq␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓␈αThe␈αimplementation␈αof␈αthe␈αtruth␈αvalues␈α␈↓
t␈↓␈α
and␈α␈↓
f␈↓␈αis␈αusually␈αsimplified,␈αmapping␈α␈↓
f␈↓␈αonto␈α
␈↓αNIL␈↓,␈αbut
␈↓ ↓H␈↓␈↓ α_allowing anything ␈↓↓but␈↓ ␈↓αNIL␈↓ as a representation of ␈↓
t␈↓.  This allows several related tricks:

␈↓ ↓H␈↓␈↓ ∧≤any expression may be used as a predicate, and 

␈↓ ↓H␈↓␈↓ βhused as a predicate, ␈↓αnot[null[l]]␈↓ has the same effect as ␈↓αl␈↓. 

␈↓ ↓H␈↓␈↓↓3.␈↓␈α∞Several␈α∂implementations␈α∞of␈α∂conditional␈α∞expressions␈α∞allow␈α∂"p␈↓βi␈↓"␈α∞as␈α∂an␈α∞abbreviation␈α∂for␈α∞"p␈↓βi␈↓ → p␈↓βi␈↓".
␈↓ ↓H␈↓␈↓ α_The computational effect is the same, but p␈↓βi␈↓ is only evaluated once.

␈↓ ↓H␈↓For␈α∞example,␈α∞an␈α∞extended␈α∞version␈α∞of␈α∞the␈α∞predicate␈α∞␈↓αmember␈↓␈α∞can␈α∞be␈α∞written.␈α∞ This␈α∞"predicate"␈α
will
␈↓ ↓H␈↓return ␈↓
f␈↓ if no matching element is found, otherwise it will return the list beginning at the match.
␈↓ ↓H␈↓αmem␈↓λ'␈↓α <= λ[[x;l]␈↓ β_[null[l] → ␈↓
f␈↓α;
␈↓ ↓H␈↓α␈↓ β_ equal[x;first[l]] → l;
␈↓ ↓H␈↓α␈↓ β_ ␈↓
t␈↓α → mem␈↓λ'␈↓α[x;rest[l]]] ]

␈↓ ↓H␈↓Such␈αa␈α
feature␈αis␈α
useful␈αin␈αcontexts␈α
where␈αwe␈α
wish␈αto␈αtest␈α
for␈αthe␈α
existence␈αof␈αan␈α
object␈αand,␈α
if␈αa
␈↓ ↓H␈↓match is found, do something with the object.  The use of ␈↓αassoc␈↓ occurs in similar contexts.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 97␈↓ See the problem on hash consing on page 221.
␈↓ ↓H␈↓␈↓↓192  static structure␈↓ 15.4␈↓


␈↓ ↓H␈↓␈↓ ε↔␈↓↓Problem␈↓


␈↓ ↓H␈↓I.␈α
The␈αapplication␈α
of␈αthese␈α
tricks␈αtends␈α
to␈αlead␈α
to␈αsomewhat␈α
unesthetic␈αprograms.␈α
Typically␈αwe␈α
have
␈↓ ↓H␈↓to␈α∩test␈α∩for␈α∩existence␈α∪then,␈α∩assuming␈α∩an␈α∩instance␈α∩was␈α∪discovered,␈α∩we␈α∩have␈α∩to␈α∪perform␈α∩further
␈↓ ↓H␈↓computation on that instance␈↓π 98␈↓. Constructs like:
␈↓ ↓H␈↓α␈↓ ε<[it ␈↓ ¬& test[object] → smash[it]; ...]

␈↓ ↓H␈↓arise.␈αThe␈αobjectional␈αaspect␈αinvolves␈αthe␈αvariable␈α␈↓αit␈↓.␈αThe␈αvariable␈α␈↓αit␈↓␈αis␈αnot␈αlocal␈αto␈αthe␈α
conditional
␈↓ ↓H␈↓expression.␈α↔Either␈α↔␈↓αit␈↓␈α↔is␈α↔global:␈α↔an␈α↔unnecessarily␈α↔gratuitious␈α↔side-effect;␈α↔or␈α↔␈↓αit␈↓␈α↔is␈α↔bound␈α⊗by
␈↓ ↓H␈↓λ-expression␈α
or␈αa␈α
␈↓αprog␈↓.␈α
 In␈αeither␈α
case␈α
the␈αbinding␈α
is␈α
too␈αfar␈α
removed␈α
from␈αits␈α
useage.␈αSussman␈α
and
␈↓ ↓H␈↓Steele [Sus 76] suggest the construct:
␈↓ ↓H␈↓␈↓ ∧[␈↓αtest[<form␈↓β1␈↓>; ␈↓αλ[[x]␈↓<form␈↓β2␈↓>]; <form␈↓β3␈↓>],

␈↓ ↓H␈↓with␈α∂the␈α∂following␈α∂semantics:␈α∂evaluate␈α∂<form␈↓β1␈↓>;␈α∂if␈α∂it␈α∂gives␈α∂a␈α∂value␈α∂other␈α∂than␈α∂␈↓
f␈↓␈α∂then␈α∂apply␈α∂the
␈↓ ↓H␈↓second argument, a unary function, to that value. Otherwise evaluate <form␈↓β3␈↓>.

␈↓ ↓H␈↓Recast the ␈↓αisspec␈↓-argument of page 160 in terms of ␈↓αtest␈↓.

␈↓ ↓H␈↓Give an implementation of ␈↓αtest␈↓ by extending one of our evaluators.



␈↓ ↓H␈↓␈↓ ∧Y␈↓↓5.5  Symbol tables and property-lists␈↓


␈↓ ↓H␈↓Since␈αwe␈αare␈α
looking␈αat␈αimplementation␈αdetails,␈α
and␈αsince␈αmanipulation␈αof␈α
symbol␈αtables␈αis␈α
such␈αa
␈↓ ↓H␈↓central␈α∞issue␈α∞in␈α∂evaluation,␈α∞we␈α∞should␈α∂look␈α∞more␈α∞closely␈α∞at␈α∂symbol␈α∞tables␈α∞and␈α∂their␈α∞organization.
␈↓ ↓H␈↓We␈α⊂have␈α⊂seen␈α⊃two␈α⊂fundamentally␈α⊂different␈α⊂implementations␈α⊃of␈α⊂symbol␈α⊂table␈α⊃orgainzation:␈α⊂deep
␈↓ ↓H␈↓binding␈α≡and␈α∨shallow␈α≡binding.␈α∨We␈α≡should␈α∨examine␈α≡the␈α∨various␈α≡advantages␈α∨of␈α≡these
␈↓ ↓H␈↓implementations,␈α⊂and␈α⊂probe␈α⊂deeper␈α⊂into␈α⊂the␈α⊃details␈α⊂of␈α⊂their␈α⊂implementation.␈α⊂ If␈α⊂the␈α⊃number␈α⊂of
␈↓ ↓H␈↓entries␈α
associated␈αwith␈α
an␈αatom␈α
is␈α
small␈αthen␈α
shallow␈αbinding␈α
may␈α
be␈αadvantageous.␈α
 If␈αthe␈α
number
␈↓ ↓H␈↓of␈α∞entries␈α∞associated␈α∞with␈α∞an␈α∂atom␈α∞is␈α∞very␈α∞large␈α∞then␈α∂the␈α∞shallow␈α∞binding␈α∞technique␈α∞may␈α∂be␈α∞too
␈↓ ↓H␈↓costly and deep binding or yet another organization may be required ([McD 75]).

␈↓ ↓H␈↓Recall␈αour␈αdiscussion␈αof␈α␈↓αgetval␈↓␈αand␈α␈↓αaddval␈↓␈αin␈αSection 4.11.␈α These␈αtwo␈αfunctions␈αwere␈αdeveloped␈αto
␈↓ ↓H␈↓describe␈αshallow␈αbinding,␈αbut␈αthey␈αare␈αillustrative␈αof␈αa␈αmore␈αgeneral␈αidea.␈αIn␈αsymbol␈αmanipulation
␈↓ ↓H␈↓and␈αsymbolic␈αprogramming,␈αwe␈αoften␈αwant␈αto␈αbe␈αable␈αto␈αassociate␈αa␈αset␈αof␈αdata␈αwith␈αan␈αitem␈αunder
␈↓ ↓H␈↓discussion.␈αFor␈α
example,␈αan␈α
algebraic␈αsimplifier␈α
would␈αlike␈αto␈α
know␈αwhether␈α
a␈αspecific␈α
operator␈αis
␈↓ ↓H␈↓associative;␈α⊃if␈α∩so,␈α⊃certain␈α∩simplifications␈α⊃are␈α∩valid.␈α⊃We␈α⊃could␈α∩maintain␈α⊃a␈α∩list␈α⊃of␈α∩all␈α⊃associative
␈↓ ↓H␈↓operations␈α
and␈αrequire␈α
that␈αthe␈α
simplifier␈αcheck␈α
that␈α
list.␈αBut␈α
since␈αassociativity␈α
is␈αa␈α
property␈αof␈α
the
␈↓ ↓H␈↓operator␈α
it␈αseems␈α
more␈α
natural␈αto␈α
associate␈αthe␈α
property␈α
with␈αthe␈α
operation.␈α
Search␈αconsiderations
␈↓ ↓H␈↓also␈α⊂arise␈α⊂if␈α⊂the␈α⊂list␈α∂of␈α⊂operations␈α⊂is␈α⊂long.␈α⊂Indeed␈α∂the␈α⊂same␈α⊂considerations␈α⊂we␈α⊂expressed␈α⊂in␈α∂the
␈↓ ↓H␈↓discussion of shallow binding arise here; and the same solution is applicable.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 98␈↓ If no further computation is necessary, trick No. ␈↓↓3␈↓ suffices.
␈↓ ↓H␈↓␈↓↓5.5␈↓ πDSymbol tables and property-lists     193␈↓


␈↓ ↓H␈↓We␈α∂generalize␈α∞the␈α∂idea␈α∂expressed␈α∞in␈α∂␈↓αgetval␈↓␈α∂and␈α∞␈↓αaddval␈↓,␈α∂allowing␈α∂the␈α∞association␈α∂of␈α∂an␈α∞arbitrary
␈↓ ↓H␈↓collection␈α∀of␈α∀properties␈α∀and␈α∀values.␈α∀ Most␈α∀implementations␈α∀of␈α∀LISP␈α∀allow␈α∀the␈α∀association␈α∪of
␈↓ ↓H␈↓arbitrary␈αsequences␈αof␈α␈↓¬property-value␈↓␈αpairs␈αwith␈αan␈αatom.␈α With␈αeach␈αatom␈αwe␈αwill␈αassociate␈αa␈αlist
␈↓ ↓H␈↓called␈α∪the␈α∪␈↓¬property-list␈↓␈α∪or␈α∀p␈↓¬-list.␈↓␈α∪The␈α∪names␈α∪␈↓↓attribute␈↓,␈α∀or␈α∪␈↓↓indicator␈↓,␈α∪are␈α∪sometimes␈α∀used␈α∪as
␈↓ ↓H␈↓synonyms for property. An atom is frequently called a ␈↓↓carrier␈↓ of the properties.

␈↓ ↓H␈↓A␈α∂property␈α∞list␈α∂is␈α∞a␈α∂table␈α∞of␈α∂properties␈α∞and␈α∂property values.␈α∞The␈α∂size␈α∞of␈α∂the␈α∞property␈α∂list␈α∂is␈α∞not
␈↓ ↓H␈↓fixed, but can shrink and grow during a computation.

␈↓"␈↓ ↓H␈↓
        ⊂αααααααπααααα⊃
␈↓"␈↓ ↓H␈↓
        ~ prop1 ~ val1~
␈↓"␈↓ ↓H␈↓
        εαααααααβαααααλ
␈↓"␈↓ ↓H␈↓
        ~ prop2 ~ val2~
␈↓"␈↓ ↓H␈↓
             # # #
␈↓"␈↓ ↓H␈↓
        εαααααααβαααααλ
␈↓"␈↓ ↓H␈↓
        ~ propn ~ valn~
␈↓"␈↓ ↓H␈↓
        %ααααααα∀ααααα$

␈↓ ↓H␈↓We␈α∞have␈α∂seen␈α∞an␈α∂identical␈α∞diagram␈α∂in␈α∞Section 3.4;␈α∞property␈α∂lists␈α∞turn␈α∂out␈α∞to␈α∂be␈α∞a␈α∂very␈α∞effective
␈↓ ↓H␈↓means␈α↔of␈α↔modelling␈α_data␈α↔bases␈↓π 99␈↓.␈α↔ As␈α↔abstractions,␈α_property␈α↔lists␈α↔are␈α↔symbol␈α_tables.␈α↔The
␈↓ ↓H␈↓name-entries␈α∞are␈α∞properties␈α∞and␈α∂the␈α∞value␈α∞entries␈α∞are␈α∞the␈α∂property␈α∞values.␈α∞ We␈α∞identify␈α∂an␈α∞atom
␈↓ ↓H␈↓with its property list.

␈↓ ↓H␈↓For␈αexample,␈αif␈αwe␈αwished␈αto␈αrepresent␈αthe␈α"+"-operation␈αas␈αthe␈αatom␈α␈↓αPLUS␈↓␈αand␈αwished␈αto␈αsignify
␈↓ ↓H␈↓that the operation is associative and binary, the atom ␈↓αPLUS␈↓ might be represented as:

␈↓"␈↓ ↓H␈↓
PLUS    ⊂αααααααπααα⊃
␈↓"␈↓ ↓H␈↓
        ~ assoc ~ T ~
␈↓"␈↓ ↓H␈↓
        εαααααααβαααλ
␈↓"␈↓ ↓H␈↓
        ~ arity ~ 2 ~
␈↓"␈↓ ↓H␈↓
        %ααααααα∀ααα$

␈↓ ↓H␈↓In␈α
these␈αkinds␈α
of␈αapplications,␈α
we␈αare␈α
using␈αproperties␈α
associated␈αwith␈α
atom,␈αthought␈α
of␈αas␈α
a␈αdata
␈↓ ↓H␈↓structures.␈αThese␈αsame␈αtechniques␈αare␈αapplicable␈αwhen␈αwe␈αthink␈αabout␈αatoms␈αas␈αrepresentations␈αof
␈↓ ↓H␈↓variables as used in the evaluation process.














␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 99␈↓ They are by no means the ␈↓↓only␈↓ or ␈↓↓best␈↓ way of representing every data base. See [McD 75].
␈↓ ↓H␈↓%2194  static structure␈↓ _5.5%*



␈↓ ↓H␈↓In␈αfact,␈αan␈α
atom␈αcan␈αsimultaneously␈α
be␈αused␈αas␈α
a␈αcarrier␈αof␈α
a␈αvalue␈αand␈α
can␈αalso␈αhave␈α
a␈αproperty
␈↓ ↓H␈↓list:

␈↓"␈↓ ↓H␈↓
    E4
␈↓"␈↓ ↓H␈↓
    ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
  ⊂←β X ~ 3 ~
␈↓"␈↓ ↓H␈↓
  ↓ εαααβαααλ
␈↓"␈↓ ↓H␈↓
      # # #      E3
␈↓"␈↓ ↓H␈↓
  ↓ %ααα∀ααα∀→αα→⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
  ~                # # #
␈↓"␈↓ ↓H␈↓
  ↓              εαααβαααλ
␈↓"␈↓ ↓H␈↓
  %→ααα→π←αα←ααα←β X ~ A ~     E1
␈↓"␈↓ ↓H␈↓
        ↓      ↑ %ααα∀ααα∀→ααα→⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
⊂αααααααπααααα⊃~                 # # #
␈↓"␈↓ ↓H␈↓
~ prop1 ~ val1~~               εαααβαααλ
␈↓"␈↓ ↓H␈↓
      # # #    %←ααααα←ααααα←ααβ X ~ B ~
␈↓"␈↓ ↓H␈↓
εαααααααβαααααλ                εαααβαααλ
␈↓"␈↓ ↓H␈↓
~ propn ~ valn~                  # # #
␈↓"␈↓ ↓H␈↓
%ααααααα∀ααααα$                %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
  ␈↓p-list for ␈↓αX␈↓

␈↓ ↓H␈↓Thus␈α
all␈α
instances␈αof␈α
␈↓αX␈↓␈α
share␈αa␈α
common␈α
property␈αlist,␈α
but␈α
the␈αvalue,␈α
or␈α
binding␈αof␈α
␈↓αx␈↓␈α
is␈αfound␈α
using
␈↓ ↓H␈↓the␈αenvironment␈αchain.␈α This␈α
example␈αis␈αdescribed␈αin␈αterms␈α
of␈αdeep␈αbinding,␈αbut␈α
the␈αproperty-list
␈↓ ↓H␈↓idea␈α∂is␈α∂also␈α∂directly␈α∂applicable␈α∂to␈α∂the␈α∞shallow␈α∂binding␈α∂scheme.␈α∂ To␈α∂adapt␈α∂p-lists␈α∂to␈α∂this␈α∞binding
␈↓ ↓H␈↓scheme,␈α∩we␈α∩introduce␈α∪a␈α∩property␈α∩named␈α∪␈↓αVAR␈↓␈α∩whose␈α∩property␈α∩value␈α∪will␈α∩be␈α∩the␈α∪collection␈α∩of
␈↓ ↓H␈↓environment-value pairs:

␈↓"␈↓ ↓H␈↓
X ⊂αααααααπαααααα⊃     ⊂ααααπααα⊃
␈↓"␈↓ ↓H␈↓
  ~  VAR  ~    #αβα→αα→~ E4 ~ 3 ~
␈↓"␈↓ ↓H␈↓
  εαααααααβααααααλ     εααααβαααλ
␈↓"␈↓ ↓H␈↓
  ~ prop1 ~ val1 ~     ~ E3 ~ A ~
␈↓"␈↓ ↓H␈↓
  εαααααααβααααααλ     εααααβαααλ
␈↓"␈↓ ↓H␈↓
      #   #   #        ~ E1 ~ B ~
␈↓"␈↓ ↓H␈↓
  εαααααααβααααααλ     %αααα∀ααα$
␈↓"␈↓ ↓H␈↓
  ~ propn ~ valn ~
␈↓"␈↓ ↓H␈↓
  %ααααααα∀αααααα$


␈↓ ↓H␈↓In␈αsummary,␈αproperty␈αlists␈αare␈αa␈αfeature␈α
independent␈αof␈αthe␈αbinding␈αstrategy␈αwe␈αhave␈α
implemented.
␈↓ ↓H␈↓A␈α
property␈α
list␈α
contains␈α
all␈α
the␈α
aspects␈α
of␈αan␈α
atom␈α
which␈α
we␈α
wish␈α
to␈α
consider.␈α
The␈α
deep␈αbinding
␈↓ ↓H␈↓implementation␈αemphaiszes␈α
that␈αthe␈αvalue␈α
of␈αa␈αvariable␈α
is␈αssociated␈αwith␈α
the␈αenvironment␈αin␈α
which
␈↓ ↓H␈↓that␈α
value␈α
is␈α
created.␈αHowever␈α
shallow␈α
binding␈α
organization␈αassociates␈α
values␈α
with␈α
variables,␈αand
␈↓ ↓H␈↓thus it is natural to think of property-lists when thinking of shallow binding.

␈↓ ↓H␈↓We␈αwish␈αto␈αlook␈αmore␈αclosely␈αat␈αthe␈αvalue␈αaspects␈αof␈αatoms.␈α We␈αhave␈αseen␈αthree␈αproperties␈αrelated
␈↓ ↓H␈↓to␈αthe␈αvalue␈α
of␈αa␈αvariable:␈α
simple␈αvalue,␈αcall-by-value␈α
function␈α(expr)␈αand␈αcall-unevaluated␈α
function
␈↓ ↓H␈↓(fexpr).␈αWe␈αwere␈αable␈αto␈αdistinguish␈αbetween␈αexprs␈αand␈αfexprs␈αby␈αone␈αof␈αtwo␈αmethods:␈αeither␈αplace
␈↓ ↓H␈↓␈↓↓5.5␈↓ πDSymbol tables and property-lists     195␈↓


␈↓ ↓H␈↓the␈αfexpr␈αname␈αin␈αa␈αspecial␈αlist␈αor␈αstore␈αthe␈αfexpr␈αas␈αa␈αβ-expression,␈αrather␈αthan␈αas␈αa␈αλ-expression.
␈↓ ↓H␈↓We␈α⊂made␈α⊂no␈α⊂explicit␈α⊃distinction␈α⊂between␈α⊂simple␈α⊂values␈α⊂and␈α⊃function␈α⊂values;␈α⊂if␈α⊂a␈α⊃simple␈α⊂value
␈↓ ↓H␈↓appeared␈α∂in␈α⊂the␈α∂function␈α∂position␈α⊂of␈α∂an␈α∂application,␈α⊂we␈α∂evaluated␈α∂that␈α⊂expression␈α∂until␈α⊂we␈α∂␈↓↓did␈↓
␈↓ ↓H␈↓discover␈αa␈αfunction␈αobject.␈α If␈αa␈αfunction␈αappeared␈αin␈αa␈αposition␈αexpecting␈αa␈αsimple␈αvalue,␈αthen␈αthe
␈↓ ↓H␈↓data structure interpretation of the function object was taken.

␈↓ ↓H␈↓Since␈α"<="␈αand␈α"<␈↓βf␈↓="␈αwere␈αdefined␈αto␈αplace␈αthe␈αappropriate␈αfunction␈αdefinition␈αin␈αthe␈α
global␈αtable,
␈↓ ↓H␈↓we␈α
can␈α
interpret␈α
these␈α
operations␈α
as␈α
associating␈α
the␈α
definition␈α
with␈α
the␈α
atom.␈α
That␈α
is,␈α
being␈αan␈α
expr
␈↓ ↓H␈↓or␈αfexpr␈αis␈αa␈αproperty␈αof␈αthe␈αatom.␈αSimilarly,␈αglobally␈αbound␈αvariables␈αlike␈α␈↓αt␈↓␈αand␈α␈↓αnil␈↓␈αplay␈αthe␈αroles
␈↓ ↓H␈↓of␈α∞constants␈α∞and␈α∞therefore␈α∞can␈α∞be␈α∞interpreted␈α∞as␈α∞having␈α∞a␈α∞value␈α∞associated␈α∞with␈α∞them.␈α∞Primitive
␈↓ ↓H␈↓functions␈α∪like␈α∀␈↓αcar␈↓,␈α∪and␈α∪primitve␈α∀special␈α∪forms␈α∪like␈α∀␈↓αcond␈↓␈α∪should␈α∪also␈α∀be␈α∪considered␈α∀as␈α∪being
␈↓ ↓H␈↓constants.␈α∀Their␈α∀"values"␈α∪are␈α∀fixed␈α∀procedures␈α∪for␈α∀specific␈α∀call-by-value␈α∀and␈α∪call-unevaluated
␈↓ ↓H␈↓operations, respectively.

␈↓ ↓H␈↓This␈αdiscussion␈αexemplifies␈αfive␈αvalue-like␈αproperties␈αwhich␈αare␈αproperties␈αof␈αan␈αatom,␈αrather␈αthan
␈↓ ↓H␈↓properties of a particular environment␈↓π 100␈↓:

␈↓ ↓H␈↓␈↓ β(␈↓αCONST␈↓␈↓ πλsimple value; used as a constant
␈↓ ↓H␈↓␈↓ β(␈↓αEXPR␈↓␈↓ πλcall-by value definition
␈↓ ↓H␈↓␈↓ β(␈↓αFEXPR␈↓␈↓ πλcall-unevaluated definition
␈↓ ↓H␈↓␈↓ β(␈↓αSUBR␈↓␈↓ πλcall-by-value primitive
␈↓ ↓H␈↓␈↓ β(␈↓αFSUBR␈↓␈↓ πλcall-unevaluated primitive

␈↓ ↓H␈↓In␈α∪Section ␈α∪we␈α∪will␈α∪introduce␈α∪another␈α∪protocol␈α∪for␈α∪assigning␈α∪values␈α∪to␈α∪variables,␈α∪but␈α∪at␈α∩any
␈↓ ↓H␈↓one time an atom may have at most one of these value-related indicators␈↓π 101␈↓.

␈↓ ↓H␈↓For example, ␈↓αcar␈↓ might be represented as:

␈↓"␈↓ ↓H␈↓
        ⊂ααααααπααα⊃
␈↓"␈↓ ↓H␈↓
        ~ SUBR ~ #αβ→␈↓To machine language code for ␈↓αcar␈↓

␈↓"␈↓ ↓H␈↓
        %αααααα∀ααα$

␈↓ ↓H␈↓␈↓ ∧a␈↓↓Part of the atom-structure for ␈↓αCAR␈↓
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 100␈↓␈αSoon␈αwe␈αwill␈αdiscuss␈αthe␈αpossibilities␈αof␈αconsolidating␈αthe␈α␈↓αVAR␈↓␈αproperty␈αwith␈αthese␈αother␈αvalue
␈↓ ↓H␈↓properties. That will be the intent of Section 5.17.

␈↓ ↓H␈↓␈↓π 101␈↓␈α∃Several␈α∃implementations␈α∃allow␈α∃atoms␈α∃to␈α∃have␈α∃several␈α∃of␈α∃these␈α∃system␈α∃properties.␈α∃Thus
␈↓ ↓H␈↓expressions␈αlike␈α␈↓αcar[car]␈↓␈αare␈αexecutable.␈αThe␈αimplementation␈αuses␈αcontext␈αto␈αdetermine␈αwhich␈α␈↓αcar␈↓␈αis
␈↓ ↓H␈↓a␈αsimple␈α
variable␈αand␈α
which␈α␈↓αcar␈↓␈α
is␈αthe␈α
primitive␈αprocedure.␈α
 The␈αcurrent␈α
␈↓αeval␈↓␈α␈↓↓will␈↓␈αoperate␈α
correctly
␈↓ ↓H␈↓on␈α∂this␈α∞example␈α∂since␈α∞a␈α∂recognizer␈α∞for␈α∂the␈α∂function␈α∞␈↓αcar␈↓␈α∂is␈α∞explicitly␈α∂encoded␈α∞in␈α∂␈↓αapply␈↓,␈α∂but␈α∞such
␈↓ ↓H␈↓tricks␈α∞lead␈α∞to␈α∞unnecessarily␈α∞mysterious␈α∞programs.␈α∞Context␈α
is␈α∞used␈α∞by␈α∞an␈α∞evaluator␈α∞in␈α∞slightly␈α
less
␈↓ ↓H␈↓obnoxious␈α∞ways.␈α∞For␈α∞example,␈α
an␈α∞evaluator␈α∞for␈α∞␈↓αprog␈↓␈α
can␈α∞tell␈α∞a␈α∞reference␈α
to␈α∞the␈α∞label␈α∞␈↓αx␈↓␈α∞from␈α
the
␈↓ ↓H␈↓␈↓αprog␈↓-variable␈α∞␈↓αx␈↓␈α∞in␈α∞␈↓α ... prog[[x;y] .. x f[..] ... g[x .. ] ... go[x].␈↓␈α∞See␈α∞page 138.␈α∞ For␈α∞the␈α∞same␈α∞reason,␈α
the
␈↓ ↓H␈↓LISP␈α'obscenity␈α(␈↓αλ[[lambda]␈α'...␈α(]␈↓␈α'will␈α(work.␈α'Notice␈α(its␈α'S-expr␈α(representation␈α'is
␈↓ ↓H␈↓␈↓α(LAMBDA (LAMBDA)  ... )␈↓.
␈↓ ↓H␈↓␈↓↓196  static structure␈↓ 35.5␈↓


␈↓ ↓H␈↓When␈α∞we␈α∞use␈α∞␈↓αcar␈↓␈α∞as␈α∞a␈α∞function␈α∞the␈α
machine-dependent␈α∞code␈α∞will␈α∞be␈α∞executed.␈α∞When␈α∞we␈α∞use␈α
the
␈↓ ↓H␈↓atom ␈↓αCAR␈↓ we access the representation ␈↓
CAR␈↓.

␈↓ ↓H␈↓Notice␈α∂that␈α∂each␈α∂indicator␈α∞is␈α∂atomic,␈α∂so␈α∂when␈α∞we␈α∂write␈α∂␈↓
SUBR␈↓␈α∂we␈α∞actually␈α∂mean␈α∂a␈α∂pointer␈α∂to␈α∞the
␈↓ ↓H␈↓representation of the atom ␈↓αSUBR␈↓. Every use of an atom is a reference to its atomic structure.
␈↓ ↓H␈↓As a further example, we might define:

␈↓ ↓H␈↓␈↓ ∧⊗␈↓αfact <= λ[[x][x=0 → 1; ␈↓
t␈↓α → times[x;fact[sub1[x]]]]]

␈↓ ↓H␈↓α␈↓The S-expr translation of the right-hand side would be:

␈↓ ↓H␈↓␈↓ αT␈↓α(LAMBDA (X) (COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X))))))

␈↓ ↓H␈↓To␈α
represent␈α
the␈αintention␈α
that␈α
␈↓αfact␈↓␈αis␈α
to␈α
be␈α
defined␈αas␈α
the␈α
above␈αrecursive␈α
function,␈α
we␈αmight␈α
store
␈↓ ↓H␈↓the␈α
S-expr␈α
representation␈α
on␈α
the␈α
property-list␈α
of␈α
the␈α
atom␈α
␈↓αFACT␈↓␈α
and␈α
use␈α
␈↓αEXPR␈↓␈α
as␈α∞its␈α
indicator.
␈↓ ↓H␈↓The␈α∞occurrence␈α∞of␈α∞the␈α∞atom␈α∞␈↓αFACT␈↓␈α∞in␈α∞the␈α
λ-expression␈α∞is␈α∞represented␈α∞as␈α∞a␈α∞pointer␈α∞to␈α∞the␈α
atomic
␈↓ ↓H␈↓structure of ␈↓
FACT␈↓.

␈↓ ↓H␈↓Here is part of the atom-structure for ␈↓αFACT␈↓:

␈↓"␈↓ ↓H␈↓
      ⊂ααααααπαααα⊃
␈↓"␈↓ ↓H␈↓
FACT  ~ EXPR ~  #αβ→⊃
␈↓"␈↓ ↓H␈↓
  ⊂αα→%αααααα∀αααα$ ~
␈↓"␈↓ ↓H␈↓
  ↑  ⊂←αααα←ααααα←αα$
␈↓"␈↓ ↓H␈↓
  ~  %→⊂ααααααααπααα⊃   ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
  ~    ~ LAMBDA ~ #αβαα→~ # ~ #αβα→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
  ~    %αααααααα∀ααα$   %αβα∀ααα$  %αβα∀αα$
␈↓"␈↓ ↓H␈↓
  ↑                       ~          ↓
␈↓"␈↓ ↓H␈↓
  ~            ⊂αααααααααα$   ⊂ααααααπααα⊃ ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
  ~            ↓              ~ COND ~ #αβ→~ # ~ #αβ→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
  ~         ⊂αααπαα⊃          %αααααα∀ααα$ %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ ↓H␈↓
  ↑         ~ X ~≤'~                         ↓         ↓
␈↓"␈↓ ↓H␈↓
  ~         %ααα∀αα$                        ...       ...
␈↓"␈↓ ↓H␈↓
  ~                                                    ↓
␈↓"␈↓ ↓H␈↓
  ~                                              ⊂αααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
  ↑                                              ~ # ~ #αβ→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
  ~                                              %αβα∀ααα$ %αβα∀αα$
␈↓"␈↓ ↓H␈↓
  %←αααα←αααααα←αααααααα←αααααααα←αααααααα←ααααααα←$         ↓
␈↓"␈↓ ↓H␈↓
                                                ⊂←αααααα←αααα$
␈↓"␈↓ ↓H␈↓
                                                ↓
␈↓"␈↓ ↓H␈↓
                                          ⊂ααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                                          ~ SUB1 ~ #αβ→~ X ~≤'~
␈↓"␈↓ ↓H␈↓
                                          %αααααα∀ααα$ %ααα∀αα$
␈↓ ↓H␈↓␈↓ ¬#␈↓↓Atom-structure for ␈↓αFACT␈↓

␈↓ ↓H␈↓Note␈α∪that␈α∪both␈α∩instances␈α∪of␈α∪␈↓
X␈↓␈α∩are␈α∪actually␈α∪pointers␈α∩to␈α∪the␈α∪representation␈α∩of␈α∪␈↓αX␈↓,␈α∪but␈α∪the␈α∩two
␈↓ ↓H␈↓representations␈α∞of␈α∞␈↓α(X)␈↓␈α∞will␈α∞typically␈α∞be␈α∞distinct.␈α∞ Also␈α
keep␈α∞in␈α∞mind␈α∞that␈α∞we␈α∞are␈α∞storing␈α∞the␈α
data
␈↓ ↓H␈↓structure ␈↓↓representation␈↓ of the ␈↓αfact␈↓ function thus

␈↓ ↓H␈↓␈↓ αT␈↓α(LAMBDA (X) (COND ((ZEROP X) 1) (T (TIMES X (FACT (SUB1 X))))))
␈↓ ↓H␈↓␈↓↓5.5␈↓ πDSymbol tables and property-lists     197␈↓


␈↓ ↓H␈↓is␈α
a␈α
perfectly␈α
good␈α
list␈α
and␈α
therefore␈α
a␈α
data␈α
structure.␈α
If␈α
we␈α
attached␈α
it␈α
to␈α
the␈α
indicator␈α
␈↓αVALUE␈↓␈α
then
␈↓ ↓H␈↓we would have represented the list as the ␈↓↓simple␈↓ value of ␈↓αfact␈↓.

␈↓ ↓H␈↓Representations␈α
of␈α
special␈αforms␈α
like␈α
␈↓αCOND␈↓␈αand␈α
␈↓αQUOTE␈↓␈α
give␈α
rise␈αto␈α
the␈α
indicator␈α␈↓αFSUBR␈↓.␈α
 When
␈↓ ↓H␈↓an␈α⊃instance␈α⊃of␈α⊂such␈α⊃a␈α⊃special␈α⊂form␈α⊃is␈α⊃recognized,␈α⊂the␈α⊃argument␈α⊃list␈α⊂is␈α⊃passed␈α⊃to␈α⊃the␈α⊂primitive
␈↓ ↓H␈↓without␈αany␈αevaluation.␈α In␈αa␈αsimilar␈αmanner␈αwe␈αintroduce␈αthe␈αindicator␈α␈↓αFEXPR␈↓␈αto␈αdesignate␈αthe
␈↓ ↓H␈↓occurrence of a "<␈↓βf␈↓=" definition.

␈↓ ↓H␈↓Our␈α∪discussion␈α∩of␈α∪property␈α∩lists␈α∪as␈α∩carriers␈α∪of␈α∩values␈α∪has␈α∩centered␈α∪on␈α∩the␈α∪representations␈α∩of
␈↓ ↓H␈↓constants.␈α∞Many␈α
identifiers␈α∞in␈α
LISP␈α∞tend␈α∞to␈α
be␈α∞constants;␈α
even␈α∞though␈α
we␈α∞can␈α∞redefine␈α
functions
␈↓ ↓H␈↓using␈α
a␈α
version␈α
of␈α
"<=",␈α
most␈α
such␈α
definitions␈α
are␈α
relatively␈α
constant␈↓π 102␈↓.␈α
The␈α
primitve␈α
functions␈α
are
␈↓ ↓H␈↓also␈αconstant.␈αShallow␈αbinding␈αtries␈αto␈αcapitalize␈αof␈αthis␈αobservation,␈αby␈αassociating␈αall␈αof␈αthe␈αvalue
␈↓ ↓H␈↓aspects␈αof␈αa␈αvariable␈αwith␈αthe␈αproperty␈αlist,␈αand␈αwe␈αwill␈αsoon␈αsee␈αthat␈αfor␈αseveral␈αinteresting␈αsubsets
␈↓ ↓H␈↓of LISP, we can significantly simplify the handling of shallow binding.

␈↓ ↓H␈↓Before␈α∂discussing␈α∞that␈α∂we␈α∂will␈α∞show␈α∂how␈α∞an␈α∂evaluator␈α∂might␈α∞use␈α∂such␈α∂property-list␈α∞information.
␈↓ ↓H␈↓That requires introduction of property-list manipulating functions.



␈↓ ↓H␈↓␈↓ ¬~␈↓↓5.6  Property-list functions␈↓


␈↓ ↓H␈↓There are four functions for manipulating the property-list:

␈↓ ↓H␈↓␈↓αputprop[a;v;p]:␈↓␈α␈↓αputprop␈↓␈αwill␈αput␈αthe␈αvalue␈α␈↓αv␈↓␈αunder␈αthe␈αproperty␈α␈↓αp␈↓␈αon␈αthe␈αproperty-list␈αof␈αthe␈αatom
␈↓ ↓H␈↓␈↓ β_␈↓αa␈↓.␈α∞If␈α∂the␈α∞property␈α∂␈↓αp␈↓␈α∞already␈α∂appears␈α∞on␈α∂the␈α∞p-list␈α∂then␈α∞the␈α∂␈↓αv␈↓␈α∞over-writes␈α∂the␈α∞old
␈↓ ↓H␈↓␈↓ β_value;␈αotherwise␈αa␈αnew␈αproperty-value␈αpair␈αis␈αadded␈αto␈αthe␈αfront␈αof␈αthe␈αp-list␈α
of␈α␈↓αa␈↓.
␈↓ ↓H␈↓␈↓ β_The value returned by ␈↓αputprop␈↓ is ␈↓αv␈↓.

␈↓ ↓H␈↓␈↓αget[x;i]:␈α␈↓␈α␈↓αget␈↓␈αwill␈αsearch␈αthe␈αproperty-list␈αof␈αthe␈αatom␈α␈↓αx␈↓␈αlooking␈αfor␈αthe␈αindicator␈α␈↓αi␈↓.␈αIf␈α␈↓αi␈↓␈αis␈αfound␈αthe
␈↓ ↓H␈↓␈↓ αHvalue␈αassociated␈αwith␈αthat␈αindicator␈αis␈αreturned␈αby␈α␈↓αget␈↓.␈α If␈α␈↓αx␈↓␈αdoes␈αnot␈αhave␈α
the␈αindicator
␈↓ ↓H␈↓␈↓ αHthen ␈↓
f␈↓ is returned.  Thus ␈↓αgetval[x]␈↓ could be defined as ␈↓αget[x;VAR].

␈↓ ↓H␈↓␈↓αgetl[x;l]:␈↓␈α⊂␈↓αl␈↓␈α⊂is␈α⊂a␈α⊂list␈α⊂of␈α⊂indicators.␈α⊂ ␈↓αgetl␈↓␈α⊃will␈α⊂search␈α⊂the␈α⊂property-list␈α⊂of␈α⊂the␈α⊂atom␈α⊂␈↓αx␈↓␈α⊂for␈α⊃the␈α⊂␈↓↓first␈↓
␈↓ ↓H␈↓␈↓ αHoccurrence␈α⊂of␈α⊂any␈α⊃indicator␈α⊂which␈α⊂appears␈α⊂in␈α⊃␈↓αl␈↓.␈α⊂ If␈α⊂such␈α⊂a␈α⊃match␈α⊂is␈α⊂found,␈α⊃then␈α⊂the
␈↓ ↓H␈↓␈↓ αH␈↓↓remainder␈↓␈αof␈αthe␈αp-list,␈αbeginning␈αwith␈αthe␈αmatching␈αindicator,␈αis␈αreturned.␈α If␈αno␈αmatch
␈↓ ↓H␈↓␈↓ αHis␈α∞found,␈α∂␈↓
f␈↓␈α∞is␈α∂returned.␈α∞ The␈α∂virtue␈α∞of␈α∞␈↓αgetl␈↓␈α∂is␈α∞that␈α∂it␈α∞can␈α∂distinguish␈α∞between␈α∂an␈α∞atom
␈↓ ↓H␈↓␈↓ αHwhich␈αhas␈α
an␈αindicator␈αwith␈α
value␈α␈↓
f␈↓␈αand␈α
an␈αatom␈α
which␈αdoes␈αnot␈α
have␈αthe␈αindicator.␈α
 ␈↓αget␈↓
␈↓ ↓H␈↓␈↓ αHcannot␈α
communicate␈α
this␈α
distinction.␈α
 The␈α
disadvantage␈α
of␈α
␈↓αgetl␈↓␈α
is␈α
that␈α
it␈α
gives␈α∞access␈α
to
␈↓ ↓H␈↓␈↓ αHthe␈αinternal␈αstructure,␈αand␈αtherefore␈αthe␈αrepresentation,␈αof␈αthe␈αatom.␈αSuch␈αp-list␈αfunctions
␈↓ ↓H␈↓␈↓ αHare useful but dangerous ([Sam 75]).


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 102␈↓ To define a temporary function we use ␈↓αlabel␈↓.
␈↓ ↓H␈↓␈↓↓198  static structure␈↓ 45.6␈↓


␈↓ ↓H␈↓␈↓αremprop[n;p]␈↓:␈α
The␈αfinal␈α
function␈αin␈α
this␈α
class␈αis␈α
used␈αto␈α
remove␈α
property-value␈αpairs␈α
from␈αthe␈α
p-list
␈↓ ↓H␈↓␈↓ βλof␈α∂an␈α∂atom.␈α⊂The␈α∂function␈α∂is␈α⊂named␈α∂␈↓αremprop␈↓.␈α∂ ␈↓αremprop␈↓␈α⊂has␈α∂two␈α∂arguments:␈α⊂␈↓αn␈↓,␈α∂an
␈↓ ↓H␈↓␈↓ βλatom;␈α∂and␈α∂␈↓αp␈↓,␈α∂a␈α∂property.␈α∞If␈α∂the␈α∂property␈α∂is␈α∂found␈α∞on␈α∂the␈α∂p-list␈α∂of␈α∂the␈α∂atom,␈α∞then
␈↓ ↓H␈↓␈↓ βλ␈↓αremprop␈↓ removes the property-value pair and returns ␈↓
t␈↓; otherwise it returns ␈↓
f␈↓.



␈↓ ↓H␈↓␈↓ ¬@␈↓↓5.7  An ␈↓αeval␈↓ for p-lists␈↓α


␈↓ ↓H␈↓The␈αevaluators␈αin␈αthis␈αsection␈αillustrate␈αthe␈αuse␈αof␈αproperty␈αlists␈αand␈αintroduce␈αthe␈αfirst␈αnon-trivial
␈↓ ↓H␈↓application␈α∂of␈α∂LISP's␈α∂ability␈α⊂to␈α∂interchange␈α∂program␈α∂with␈α⊂data.␈α∂ Though␈α∂this␈α∂chapter␈α⊂is␈α∂mostly
␈↓ ↓H␈↓concerned␈αwith␈αthe␈α␈↓↓static␈↓␈α
organization␈αof␈αLISP,␈αthe␈αideas␈α
involved␈αin␈αthe␈αevaluator␈αare␈α
sufficiently
␈↓ ↓H␈↓important␈αand␈αdemonstrative␈αof␈αthe␈αpower␈αof␈αproperty␈αlists␈αthat␈αwe␈αinclude␈αthem␈αhere␈αrather␈αthan
␈↓ ↓H␈↓later.

␈↓ ↓H␈↓The␈α⊗first␈α⊗evaluator␈α⊗uses␈α∃property␈α⊗names␈α⊗like␈α⊗␈↓αCONST␈↓␈α∃and␈α⊗␈↓αEXPR␈↓␈α⊗as␈α⊗names␈α⊗of␈α∃functions.
␈↓ ↓H␈↓Discovering␈α
that␈α
an␈α
atom␈α
has␈α
the␈α␈↓αCONST␈↓␈α
property,␈α
the␈α
evaluator␈α
␈↓↓applies␈↓␈α
the␈α
function␈αnamed␈α
␈↓αconst␈↓
␈↓ ↓H␈↓to␈α
perform␈α
the␈α
evaluation.␈α
In␈α
this␈α
case,␈α
the␈α
evaluation␈α
is␈α
simple:␈α
return␈α
the␈α
represented␈α
constant.␈α
We
␈↓ ↓H␈↓will␈αassume␈αa␈αshallow␈αbinding␈αimplementation␈αand␈αtherefore␈αvariables␈αare␈αhandled␈αby␈αrecognizing
␈↓ ↓H␈↓the␈αproperty␈α␈↓αVAR␈↓␈αand␈αpassing␈αthe␈αevaluation␈αto␈αthe␈αfunction␈α␈↓αvar␈↓.␈αIn␈αthe␈αcase␈αof␈αan␈αapplication,␈αwe
␈↓ ↓H␈↓have␈α∂more␈α⊂work␈α∂to␈α⊂do;␈α∂␈↓αexpr␈↓␈α∂handles␈α⊂that.␈α∂ The␈α⊂actual␈α∂application,␈α∂is␈α⊂handled␈α∂by␈α⊂using␈α∂LISP's
␈↓ ↓H␈↓computed function facility discussed on page 175.

␈↓ ↓H␈↓We␈α
will␈α
describe␈α
a␈α
sequence␈α
of␈α
evaluators␈αbased␈α
on␈α
property␈α
list␈α
manipulation.␈α
 We␈α
will␈αnot␈α
express
␈↓ ↓H␈↓all of the details of this family of evaluators, but leave many of the details to the reader.

␈↓ ↓H␈↓αeval  <= λ[[exp;env]␈↓ βX[atom[exp] → name[first[getl[exp;(VAR CONST)]]] [exp;env];
␈↓ ↓H␈↓α␈↓ βX atom[first[exp]] → name[first[getl[first[exp];(EXPR FEXPR)]]] [exp;env];
␈↓ ↓H␈↓α␈↓ βX ... ]]

␈↓ ↓H␈↓αvar <= λ[[form;env] lookup[form;env]]

␈↓ ↓H␈↓αconst <= λ[[form;env] denote[form;env]]

␈↓ ↓H␈↓αexpr <= λ[[form;env]␈↓ βXλ[[def][eval[␈↓ ∧xbody[def];
␈↓ ↓H␈↓α␈↓ βX␈↓ ∧xmkenv[vars[def];evlist[args[form];env]] ]
␈↓ ↓H␈↓α␈↓ βX  [get[func[form];EXPR]]

␈↓ ↓H␈↓It's␈α∂not␈α∂clear␈α∂yet␈α∂that␈α∂any␈α∂substantial␈α∂benefit␈α∂has␈α∂been␈α∂gained.␈α∂ With␈α∂a␈α∂slight␈α∂change,␈α⊂we␈α∂could
␈↓ ↓H␈↓extract␈αa␈αsmall␈αimprovement.␈αReplace␈α
the␈αexplicit␈αlists␈α␈↓α(VAR CONST)␈↓␈αand␈α
␈↓α(EXPR FEXPR)␈↓␈αwith
␈↓ ↓H␈↓␈↓αidprop␈↓␈α∞and␈α∂␈↓αappprop␈↓.␈α∞Bind␈α∂these␈α∞variables␈α∂globally␈α∞to␈α∂the␈α∞respective␈α∂lists.␈α∞Then␈α∂if␈α∞we␈α∂wished␈α∞to
␈↓ ↓H␈↓define␈αa␈αnew␈αkind␈αof␈α
calling␈αsequence,␈αsay␈α␈↓αgexpr␈↓s,␈αwe␈αcould␈α
add␈α␈↓αGEXPR␈↓␈αto␈α␈↓αappprop␈↓␈αand␈α
write␈αa
␈↓ ↓H␈↓function␈αnamed␈α␈↓αgexpr␈↓␈αto␈αhandle␈αthe␈αevaluation␈αof␈α␈↓αgexpr␈↓␈αforms.␈αHowever␈αwith␈αfurther␈αanalysis,␈αwe
␈↓ ↓H␈↓can do much better.
␈↓ ↓H␈↓␈↓↓5.7␈↓ 	⊃An ␈↓αeval␈↓ for p-lists     199␈↓α


␈↓ ↓H␈↓Consider␈α
simple␈αvariables.␈α
Each␈α
instance␈αof␈α
a␈α
simple␈αvariable␈α
has␈α
the␈αsame␈α
value␈α
property;␈αwhen
␈↓ ↓H␈↓we␈αsee␈α␈↓αx␈↓␈α
we␈αapply␈α␈↓αlookup␈↓␈αthrough␈α
␈↓αvar␈↓;␈αwhen␈αwe␈α
see␈α␈↓αy␈↓␈αwe␈αapply␈α
␈↓αlookup␈↓␈αthrough␈α␈↓αvar␈↓.␈α
However␈αthe
␈↓ ↓H␈↓association␈α⊃of␈α⊃a␈α⊃value␈α⊃property␈α⊃with␈α⊃each␈α⊃␈↓↓instance␈↓␈α⊃of␈α⊃a␈α⊃variable␈α⊃is␈α⊃discomforting.␈α∩The␈α⊃value
␈↓ ↓H␈↓property␈αis␈αmore␈αa␈αproperty␈αof␈αthe␈αclass␈αof␈αvariables␈αthan␈αit␈αis␈αa␈αproperty␈αof␈αan␈αinstance.␈α That␈αis,
␈↓ ↓H␈↓an instance receives a property by being a member of a certain class.

␈↓ ↓H␈↓Let␈αthe␈αatom␈α␈↓αVAR␈↓␈αrepresent␈αthe␈αclass␈αof␈αvariables;␈αlet␈αthe␈αatom␈α␈↓αEVAL␈↓␈αrepresent␈αthe␈αproperty␈αname
␈↓ ↓H␈↓describing␈α
value␈α
properties.␈α
 The␈αfunction␈α
␈↓αlookup␈↓␈α
is␈α
therefore␈α
a␈αproperty␈α
value␈α
of␈α
the␈α
atom␈α␈↓αVAR␈↓,
␈↓ ↓H␈↓and should be associated with the property name ␈↓αEVAL␈↓.

␈↓"␈↓ ↓H␈↓
X ⊂αααααααπαααααα⊃     ⊂ααααπααα⊃   VAR ⊂ααααααπαααααααα⊃
␈↓"␈↓ ↓H␈↓
  ~  VAR  ~    #αβα→αα→~ E4 ~ 3 ~       ~ EVAL ~ LOOKUP ~
␈↓"␈↓ ↓H␈↓
  εαααααααβααααααλ     εααααβαααλ       %αααααα∀αααααααα$
␈↓"␈↓ ↓H␈↓
  ~ prop1 ~ val1 ~     ~ E3 ~ A ~
␈↓"␈↓ ↓H␈↓
  εαααααααβααααααλ     εααααβαααλ
␈↓"␈↓ ↓H␈↓
      #   #   #        ~ E1 ~ B ~
␈↓"␈↓ ↓H␈↓
  εαααααααβααααααλ     %αααα∀ααα$
␈↓"␈↓ ↓H␈↓
  ~ propn ~ valn ~
␈↓"␈↓ ↓H␈↓
  %ααααααα∀αααααα$


␈↓ ↓H␈↓Now␈αthe␈αevaluator␈αshould␈αdo␈αa␈αdouble␈α␈↓αget␈↓,␈αlooking␈αdown␈αthe␈αproperty␈αlist␈αof␈αan␈αobject,␈αfor␈αa␈αclass
␈↓ ↓H␈↓name␈αwhich␈αhas␈αan␈α␈↓αEVAL␈↓␈αproperty.␈α Finding␈α␈↓αEVAL␈↓,␈αthe␈αevaluator␈αapplies␈αthe␈αassociated␈αproperty
␈↓ ↓H␈↓value to the object and the environment.

␈↓ ↓H␈↓Before␈α
presenting␈α
the␈α
next␈α
evaluator␈α
we␈α
should␈α
settle␈α
one␈α
more␈α
point.␈α
 In␈α
the␈α
preceeding␈α
␈↓αeval␈↓␈α
we
␈↓ ↓H␈↓ignored␈α∀the␈α∀question␈α∀of␈α∀anonymous␈α∃λ-expressions;␈α∀we␈α∀assumed␈α∀that␈α∀the␈α∀function-part␈α∃of␈α∀an
␈↓ ↓H␈↓application␈αwas␈αan␈αatom.␈αWe␈αdid␈αthis␈αbecaue␈αwe␈αhave␈αimplied␈αthat␈αonly␈αatoms␈αhave␈αproperty␈αlists.
␈↓ ↓H␈↓We␈α∂will␈α⊂remove␈α∂this␈α∂restriction␈α⊂for␈α∂the␈α⊂next␈α∂evaluator␈α∂and␈α⊂assume␈α∂that␈α∂any␈α⊂object␈α∂can␈α⊂have␈α∂a
␈↓ ↓H␈↓property␈αlist.␈αA␈αλ-expression␈αwill␈αhave␈αa␈αproperty␈αlist␈αwith␈α(at␈αleast)␈αproperty␈αname␈α␈↓αLAMBDA␈↓␈αand
␈↓ ↓H␈↓property␈α
value␈α
of␈α
the␈αrepesentation␈α
of␈α
the␈α
λ-expression.␈α
The␈αatom␈α
␈↓αLAMBDA␈↓␈α
will␈α
have␈α
an␈α␈↓αEVAL␈↓
␈↓ ↓H␈↓property␈α∀whose␈α∃value␈α∀is␈α∀the␈α∃function␈α∀␈↓αeval_λ␈↓;␈α∀this␈α∃function␈α∀will␈α∀evaluate␈α∃applications␈α∀whose
␈↓ ↓H␈↓function-parts is a λ-expression.

␈↓ ↓H␈↓Finally,␈α∞since␈α∞␈↓αeval_λ␈↓␈α∞handles␈α∞most␈α
of␈α∞the␈α∞evaluation␈α∞of␈α∞an␈α
application,␈α∞there␈α∞is␈α∞no␈α∞need␈α∞to␈α
make
␈↓ ↓H␈↓␈↓αexpr␈↓␈α
do␈α
it␈α
too.␈α
In␈α
fact,␈α
our␈α
previous␈α
distinction␈α
between␈α
␈↓αVAR␈↓␈α
and␈α
␈↓αEXPR␈↓␈α
is␈αunnecessarily␈α
restrictive.
␈↓ ↓H␈↓We␈αshould␈α
be␈αable␈αto␈α
return␈αfunctions␈αas␈α
values␈αjust␈αas␈α
we␈αcan␈αreturn␈α
constants␈αor␈α
simple␈αvalues.
␈↓ ↓H␈↓So␈α∂␈↓αEXPR␈↓␈α⊂should␈α∂be␈α∂a␈α⊂property␈α∂name,␈α∂with␈α⊂property␈α∂value␈α∂being␈α⊂the␈α∂λ-expression,␈α⊂but␈α∂␈↓αEXPR␈↓
␈↓ ↓H␈↓should now have an ␈↓αEVAL␈↓ property which is just ␈↓αlookup␈↓.
␈↓ ↓H␈↓%2200  static structure␈↓ _5.7%*



␈↓ ↓H␈↓For example:

␈↓"␈↓ ↓H␈↓
     plist for FACT    plist for (LAMBDA (X) (COND ((ZEROP X) ... )))
␈↓"␈↓ ↓H␈↓
     ⊂ααααααπααα⊃       ⊂ααααααααπααα⊃
␈↓"␈↓ ↓H␈↓
     ~ EXPR ~ #αβ→ααααα→~ LAMBDA ~ #αβ→αα→ (LAMBDA (X) (COND ((ZEROP X) ...
␈↓"␈↓ ↓H␈↓
     εααααααβαααλ       εααααααααβαααλ
␈↓"␈↓ ↓H␈↓
          # # #                # # #
␈↓"␈↓ ↓H␈↓
     %αααααα∀ααα$       %αααααααα∀ααα$


␈↓"␈↓ ↓H␈↓
     plist for EXPR      plist for LAMBDA
␈↓"␈↓ ↓H␈↓
     ⊂ααααααπαααααααα⊃   ⊂ααααααπααααααααα⊃
␈↓"␈↓ ↓H␈↓
     ~ EVAL ~ LOOKUP ~   ~ EVAL ~ EVALLAM ~
␈↓"␈↓ ↓H␈↓
     εααααααβααααααααλ   εααααααβαααααααααλ
␈↓"␈↓ ↓H␈↓
          # # #                # # #
␈↓"␈↓ ↓H␈↓
     %αααααα∀αααααααα$   %αααααα∀ααααααααα$


␈↓ ↓H␈↓With all this extra mechanism in place, ␈↓αeval␈↓ does absolutely nothing!

␈↓ ↓H␈↓α␈↓ ∧⊃eval  <= λ[[exp;env] getget[exp;EVAL] [exp;env]],

␈↓ ↓H␈↓where␈α⊂␈↓αgetget␈↓␈α∂looks␈α⊂at␈α⊂property␈α∂names␈α⊂associated␈α⊂with␈α∂␈↓αexp␈↓␈α⊂until␈α∂it␈α⊂finds␈α⊂one␈α∂which␈α⊂itself␈α⊂has␈α∂a
␈↓ ↓H␈↓property list containing ␈↓αEVAL␈↓.

␈↓ ↓H␈↓Now␈α
real␈α
progress␈α
has␈α∞been␈α
made.␈α
 The␈α
evaluation␈α
of␈α∞any␈α
expression␈α
is␈α
controlled␈α
by␈α∞a␈α
function
␈↓ ↓H␈↓associated␈αwith␈αthe␈αclass␈αto␈αwhich␈αthat␈αexpression␈αbelongs.␈αIt␈αis␈αtrivial␈αto␈αmodify␈αor␈αextend␈αsuch␈αan
␈↓ ↓H␈↓evaluator: supply the class name and the appropriate ␈↓αEVAL␈↓ property value.

␈↓ ↓H␈↓The␈α∞techinque␈α∞is␈α∂applicable␈α∞to␈α∞more␈α∂general␈α∞kinds␈α∞of␈α∞computation␈α∂than␈α∞just␈α∞evaluation.␈α∂With␈α∞a
␈↓ ↓H␈↓class␈α
name␈α
we␈α
can␈α
associate␈α
arbitrary␈α
pairs␈α
of␈α
properties␈α
and␈α
functions.␈α
For␈α
example,␈α
we␈α
might␈α
wish
␈↓ ↓H␈↓to␈α∞define␈α
special␈α∞input␈α∞or␈α
output␈α∞conventions␈α∞for␈α
classes;␈α∞to␈α
do␈α∞this␈α∞we␈α
simply␈α∞associate␈α∞a␈α
␈↓αREAD␈↓
␈↓ ↓H␈↓property␈α
and␈α
a␈α
␈↓αPRINT␈↓␈α
property␈α
with␈α
the␈α∞class␈α
name␈α
and␈α
supply␈α
routines␈α
to␈α
perform␈α∞the␈α
specail
␈↓ ↓H␈↓reading␈αor␈αprnting.␈αSimilarly,␈αwe␈αcan␈αassocaiate␈αa␈αcompile␈αproperty,␈αand␈αa␈αfunction␈αdescribing␈αhow
␈↓ ↓H␈↓to compile instances of this construct␈↓π 103␈↓.

␈↓ ↓H␈↓The␈α⊃net␈α⊃effect␈α⊃of␈α⊂this␈α⊃reworking␈α⊃of␈α⊃evaluation␈α⊂is␈α⊃to␈α⊃expose␈α⊃a␈α⊂much␈α⊃more␈α⊃general␈α⊃scheme␈α⊂for
␈↓ ↓H␈↓handling␈α∃computation.␈α∃ Such␈α∃a␈α∀distributed␈α∃␈↓αeval␈↓␈α∃is␈α∃an␈α∀example␈α∃of␈α∃a␈α∃LISP␈α∃technique␈α∀called
␈↓ ↓H␈↓data-driven programming ([San 75a])␈↓π 104␈↓.
␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 103␈↓␈α⊂The␈α∂language␈α⊂EL1␈α∂([EL1 74])␈α⊂incorporates␈α∂a␈α⊂similar␈α∂scheme,␈α⊂however␈α∂only␈α⊂five␈α∂designated
␈↓ ↓H␈↓properties␈α
can␈α
be␈α
associated␈α
with␈α
any␈αclass␈α
name.␈α
The␈α
techniques␈α
of␈α
syntax-directed␈αinput-output,
␈↓ ↓H␈↓developed in Section 6.5, are also applicable to such an evaluator.

␈↓ ↓H␈↓␈↓π 104␈↓␈α
The␈αauthor␈α
first␈α
saw␈αthis␈α
technique␈α
used␈αin␈α
a␈α
non-trivial␈αway␈α
in␈α
[Dif 71]␈αin␈α
the␈αStanford␈α
LISP
␈↓ ↓H␈↓compiler
␈↓ ↓H␈↓␈↓↓5.7␈↓ 	⊃An ␈↓αeval␈↓ for p-lists     201␈↓α


␈↓ ↓H␈↓***DO DATA BASE EXAMPLE****



␈↓ ↓H␈↓␈↓ ∧\␈↓↓5.8  Representation of property-lists␈↓


␈↓ ↓H␈↓In␈α⊂discussing␈α∂representations,␈α⊂we␈α∂must␈α⊂keep␈α∂the␈α⊂essential␈α∂characteristics␈α⊂of␈α∂property␈α⊂lists␈α⊂well␈α∂in
␈↓ ↓H␈↓mind.␈α
 A␈α
property␈α
list␈α
is␈α
similar␈α
to␈α
a␈α∞local␈α
environment␈α
block;␈α
each␈α
property␈α
list␈α
is␈α
a␈α∞sequence␈α
of
␈↓ ↓H␈↓names␈α∃and␈α∃values.␈α∃However␈α∀a␈α∃property␈α∃list␈α∃can␈α∀grow␈α∃and␈α∃shrink␈α∃dynamically,␈α∃whereas␈α∀an
␈↓ ↓H␈↓environment␈α
block␈αis␈α
created␈α
at␈αa␈α
fixed␈α
size.␈αSince␈α
we␈α
cannot␈αpredict␈α
the␈α
size␈αof␈α
the␈α
block,␈αa␈α
natural
␈↓ ↓H␈↓representation is that of a list.

␈↓"␈↓ ↓H␈↓
  ⊂αααααααπααααα⊃
␈↓"␈↓ ↓H␈↓
  ~ prop1 ~ val1~
␈↓"␈↓ ↓H␈↓
  εαααααααβαααααλ
␈↓"␈↓ ↓H␈↓
  ~ prop2 ~ val2~     ⊂αααααααπααα⊃  ⊂αααααπααα⊃      ⊂αααααααπααα⊃  ⊂αααααπαα⊃
␈↓"␈↓ ↓H␈↓
        # # #         ~ prop1 ~ #αβα→~ val1~ #αβ→ ...→~ propn ~ #αβα→~ valn~≤'~
␈↓"␈↓ ↓H␈↓
  εαααααααβαααααλ     %ααααααα∀ααα$  %ααααα∀ααα$      %ααααααα∀ααα$  %ααααα∀αα$
␈↓"␈↓ ↓H␈↓
  ~ propn ~ valn~
␈↓"␈↓ ↓H␈↓
  %ααααααα∀ααααα$

␈↓"␈↓ ↓H␈↓
   ␈↓↓Property list                                      Representation␈↓


␈↓ ↓H␈↓The␈α
elements␈α
of␈α
the␈αp-list␈α
are␈α
associated␈α
in␈α
pairs.␈αThe␈α
first␈α
element␈α
of␈αa␈α
pair␈α
is␈α
the␈α
property,␈αthe
␈↓ ↓H␈↓next␈α
element␈α
is␈α
the␈α
property␈α
value.␈α
 But␈α
atoms␈α
can't␈α
be␈α
represented␈α
as␈α
just␈α
any␈α
arbitrary␈α
list.␈α
We
␈↓ ↓H␈↓must be able to recognize the occurrence of an atom so that we can implement the predicate ␈↓αatom␈↓.

␈↓ ↓H␈↓There␈α∞are␈α∞at␈α∞least␈α∞two␈α∞alternatives.␈α∞We␈α∞might␈α∞partition␈α∞memory␈α∞as␈α∞we␈α∞began␈α∞to␈α∞do␈α∂on␈α∞page 185
␈↓ ↓H␈↓with␈α∂pointer␈α∂space␈α⊂and␈α∂atom␈α∂space.␈α⊂ Then␈α∂the␈α∂test␈α∂of␈α⊂atom-ness␈α∂is␈α∂a␈α⊂test␈α∂on␈α∂the␈α⊂location␈α∂being
␈↓ ↓H␈↓referenced.␈α∞ We␈α
might␈α∞also␈α
preface␈α∞the␈α
property␈α∞list␈α
with␈α∞an␈α
"atom header"␈α∞which␈α
will␈α∞signal␈α
the
␈↓ ↓H␈↓beginning␈α
of␈α
the␈α
atom.␈α
Here␈α
the␈α
test␈α
for␈α∞atom-ness␈α
is␈α
a␈α
test␈α
on␈α
the␈α
contents␈α
of␈α
the␈α∞location␈α
being
␈↓ ↓H␈↓referenced.␈α
 In␈α
the␈α
first␈α
case␈α
we␈α
dedicate␈α
a␈α
section␈α
of␈α
memory␈α
for␈α
the␈α
storage␈α
of␈α
atoms␈↓π 105␈↓;␈α
in␈α
the
␈↓ ↓H␈↓second case we require an extra memory reference.

␈↓ ↓H␈↓A␈α∞related␈α∞efficiency␈α
consideration␈α∞involves␈α∞the␈α
use␈α∞of␈α∞property␈α
lists␈α∞by␈α∞the␈α∞LISP␈α
implementation.
␈↓ ↓H␈↓Since␈αthe␈αevaluator␈αwill␈αbe␈αmaking␈αfrequent␈αexamination␈αof␈αthe␈αp-list,␈αit␈αis␈αoften␈αuseful␈αto␈αstore␈αthe
␈↓ ↓H␈↓system-related␈α⊂information␈α∂in␈α⊂specific␈α⊂relative␈α∂positions␈α⊂of␈α∂the␈α⊂p-list.␈α⊂This␈α∂will␈α⊂obviate␈α⊂a␈α∂search
␈↓ ↓H␈↓using ␈↓αget␈↓.

␈↓ ↓H␈↓Using␈α∂a␈α⊂separate␈α∂"atom␈α⊂space",␈α∂an␈α⊂atom␈α∂would␈α∂be␈α⊂represented␈α∂by␈α⊂its␈α∂property␈α⊂list.␈α∂In␈α⊂this␈α∂case,
␈↓ ↓H␈↓property␈αlists␈αneed␈αnot␈αbe␈αstored␈αin␈αpointer␈αspace.␈αChapter ␈αexamines␈αsome␈αof␈αthese␈α
techniques.␈αIn
␈↓ ↓H␈↓the␈αtext␈α
we␈αwill␈αdescribe␈α
atoms␈αusing␈αthe␈α
"atom␈αheader"␈αsince␈α
it␈αmakes␈αit␈α
clearer␈αin␈αpictures.␈α
 Atoms
␈↓ ↓H␈↓will␈α⊂be␈α⊂special␈α⊂lists␈α⊃whose␈α⊂␈↓αcar␈↓-part␈α⊂contains␈α⊂an␈α⊂indicator␈α⊃used␈α⊂exclusively␈α⊂for␈α⊂the␈α⊃beginning␈α⊂of

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 105␈↓␈αWe␈αneed␈αnot␈αdedicate␈α
a␈αwhole␈αsection␈αof␈αthe␈α
machine␈αto␈α"atom␈αspace".␈αSeveral␈α
techniques␈αare
␈↓ ↓H␈↓available for "mapping" a conceptually contiguous space onto a scattered memory.
␈↓ ↓H␈↓␈↓↓202  static structure␈↓ 35.8␈↓


␈↓ ↓H␈↓atoms.␈αWe␈αwill␈αuse␈α␈↓
∃␈↓␈αto␈αdesignate␈αthe␈αbeginning␈αof␈αan␈αatom.␈α The␈α␈↓αcdr␈↓␈αof␈αthe␈αrepresentation␈αof␈αthe
␈↓ ↓H␈↓atom␈αis␈αthe␈αrepresentation␈αof␈αthe␈αproperty␈αlist.␈α
Such␈αlocations␈αin␈αpointer␈αspace␈αcontaining␈α␈↓
∃␈↓␈αin␈α
their
␈↓ ↓H␈↓left-half and a pointer to a p-list in their right-half are called ␈↓↓atom headers␈↓.

␈↓ ↓H␈↓For example, here is part of a representation for the atom for ␈↓αcar␈↓:

␈↓"␈↓ ↓H␈↓
          ⊂ααπαααα⊃   ⊂ααααααπαααα⊃   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
          ~∃ ~  #αβαα→~ SUBR ~  #αβαα→~ # ~ # ...→
␈↓"␈↓ ↓H␈↓
          %αα∀αααα$   %αααααα∀αααα$   %αβα∀ααα$
␈↓"␈↓ ↓H␈↓
                                        ~
␈↓"␈↓ ↓H␈↓
                                        %→αα→ to machine code for ␈↓αcar␈↓

␈↓ ↓H␈↓␈↓ ∧a␈↓↓Part of the atom-structure for ␈↓αCAR␈↓

␈↓ ↓H␈↓An␈α∞example␈α∞of␈α∞the␈α∞distinction␈α∞between␈α∞␈↓αget␈↓␈α∞and␈α∞␈↓αgetl␈↓␈α∞in␈α∞our␈α∞representation␈α∞may␈α∞be␈α∂useful.␈α∞Notice
␈↓ ↓H␈↓how both functions allow unnecessarily free access to the internal representation.

␈↓"␈↓ ↓H␈↓
 ␈↓αgetl[FOO;(BAZ)]␈↓

␈↓"␈↓ ↓H␈↓
         ⊗
␈↓"␈↓ ↓H␈↓
␈↓αgetl[FOO;(PNARF BAZ)]␈↓
                             ␈↓αget[FOO;PNARF]␈↓

␈↓"␈↓ ↓H␈↓
                    ↓                                         ↓
␈↓"␈↓ ↓H␈↓
        ⊂ααα←ααααααα$                                         ~
␈↓"␈↓ ↓H␈↓
⊂απααα⊃ ↓ ⊂αααααπααα⊃  ⊂αααααπααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃   ~
␈↓"␈↓ ↓H␈↓
~∃~ #αβαα→~ BAZ ~ #αβα→~ NIL ~ #αβα→~ PNARF ~ #αβα→~ # ~≤'~   ↓
␈↓"␈↓ ↓H␈↓
%α∀ααα$   %ααααα∀ααα$  %ααααα∀ααα$  %ααααααα∀ααα$  %αβα∀αα$   ~
␈↓"␈↓ ↓H␈↓
                                                     ε←ααα←ααα$
␈↓"␈↓ ↓H␈↓
                                                     ↓
␈↓"␈↓ ↓H␈↓
                                                 ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                                                 ~ A ~≤'~
␈↓"␈↓ ↓H␈↓
                                                 %ααα∀αα$

␈↓ ↓H␈↓and ␈↓αget[FOO;BAZ] = get[FOO;BAR] = NIL␈↓.

␈↓ ↓H␈↓The␈αsimple␈αatom␈αis␈α
becoming␈αmuch␈αmore␈αcomplex.␈α
It␈αhas␈αa␈αwhole␈α
substructure␈αattached␈αto␈αit.␈α
Thus
␈↓ ↓H␈↓each␈αatom␈αis␈αlike␈αa␈αword␈αin␈αa␈αdictionary;␈αmany␈αwords␈αcan␈αbe␈αused␈αas␈αdifferent␈αparts␈αof␈αspeech␈αand
␈↓ ↓H␈↓their␈αdictionary␈αentries␈αwill␈α
reflect␈αthis␈αby␈αhaving␈α
several␈αalternative␈αmeanings.␈αSimilarly,␈α
an␈αatom
␈↓ ↓H␈↓can␈α∂have␈α⊂several␈α∂different␈α⊂"meanings"␈α∂attached␈α∂to␈α⊂it␈α∂and␈α⊂depending␈α∂on␈α∂the␈α⊂context,␈α∂we␈α⊂will␈α∂be
␈↓ ↓H␈↓interested␈α
in␈α
one␈α
of␈αthose␈α
interpretations.␈α
 Just␈α
as␈αwe␈α
will␈α
find␈α
all␈αmeanings␈α
of␈α
a␈α
specific␈α
word␈αin
␈↓ ↓H␈↓one␈αlocation␈αin␈αthe␈αdictionary,␈αour␈αimplementation␈αof␈αLISP␈αbecomes␈αmuch␈αsimpler␈αif␈αwe␈αstore␈αeach
␈↓ ↓H␈↓atom␈α∞and␈α∞its␈α∞associated␈α∞p-list␈α∞uniquely.␈α∞For␈α∞example,␈α∞every␈α∞reference␈α∞to␈α∞the␈α∞atom␈α∞␈↓αA␈↓␈α∞is␈α∞actually␈α∞a
␈↓ ↓H␈↓pointer␈α∂to␈α∂the␈α∂same␈α∂location␈α∞in␈α∂memory.␈α∂This␈α∂location␈α∂has␈α∞a␈α∂␈↓αcar␈↓-part␈α∂which␈α∂is␈α∂the␈α∂special␈α∞atom
␈↓ ↓H␈↓indicator ␈↓
∃␈↓, and a ␈↓αcdr␈↓-part which is the p-list for the atom ␈↓αA␈↓.
␈↓ ↓H␈↓%25.8␈↓ πIRepresentation of property-lists     203%*



␈↓ ↓H␈↓Thus ␈↓α(A . A)␈↓, which we have been representing as:

␈↓"␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"␈↓ ↓H␈↓
                        ~  A  ~  A  ~
␈↓"␈↓ ↓H␈↓
                        %ααααα∀ααααα$

␈↓ ↓H␈↓in more detail, is represented as:

␈↓"␈↓ ↓H␈↓
                        ⊂αααααπααααα⊃
␈↓"␈↓ ↓H␈↓
                        ~  #  ~  #  ~
␈↓"␈↓ ↓H␈↓
                        %ααβαα∀ααβαα$
␈↓"␈↓ ↓H␈↓
                           ~     ~
␈↓"␈↓ ↓H␈↓
                           ~     ~  ⊂απααα⊃
␈↓"␈↓ ↓H␈↓
                           %ααααα∀α→~∃~ #αβα→ ␈↓p-list for ␈↓αA␈↓
.
␈↓"␈↓ ↓H␈↓
                                    %α∀ααα$

␈↓ ↓H␈↓The␈αinternal␈αstructures␈αof␈αthis␈αimplementation␈αof␈αLISP␈αare␈α␈↓↓not␈↓␈αL-trees,␈αbut␈αlist␈αstructure;␈αthere␈αare
␈↓ ↓H␈↓intersecting␈α∞and␈α∞circular␈α∞branches.␈α∞ LISP␈α∞deals␈α
with␈α∞binary␈α∞list␈α∞structure␈α∞since␈α∞each␈α
non-terminal
␈↓ ↓H␈↓node in our representation has exactly two branches.

␈↓ ↓H␈↓Assume␈α
we␈α
have␈α
the␈α
above␈α
dotted␈α
pair␈α
as␈α
a␈α
value␈α
of␈α
a␈α
variable␈α
␈↓αx␈↓␈α
and␈α
we␈α
wish␈α
to␈α
print␈α
the␈α
value␈α
of
␈↓ ↓H␈↓␈↓αx␈↓.␈α Clearly␈αwe␈αwould␈αhope␈αto␈αsee␈α"␈↓α(A␈α.␈αA)␈↓"␈αappear␈αon␈αthe␈αoutput␈αdevice.␈α We␈αwould␈αexpect␈αthat␈αthe
␈↓ ↓H␈↓print␈αroutine,␈αnamed␈α␈↓αprint␈↓,␈αwould␈αbe␈αgiven␈αa␈αpointer␈αto␈αthe␈αdotted␈αpair.␈α␈↓αprint␈↓␈αcan␈αrecognize␈αthat␈αit
␈↓ ↓H␈↓␈↓↓is␈↓␈α∞a␈α∞dotted␈α∂pair␈α∞since␈α∞its␈α∞␈↓αcar␈↓␈α∂is␈α∞not␈α∞␈↓
∃␈↓.␈α∂ But␈α∞how␈α∞can␈α∞␈↓αprint␈↓␈α∂distinguish␈α∞␈↓α(A␈α∞.␈α∞A)␈↓␈α∂from␈α∞␈↓α(B␈α∞.␈α∂B)␈↓␈α∞for
␈↓ ↓H␈↓example?␈αThe␈αpointer␈αin␈αthe␈αpreceding␈αdiagram␈αwill␈αpoint␈αto␈α␈↓αA␈↓␈αin␈αone␈αcase␈αand␈αto␈α␈↓αB␈↓␈αin␈αthe␈αother,
␈↓ ↓H␈↓but␈αnothing␈αon␈αthe␈αp-list␈αof␈αthe␈αatom␈αtells␈αus␈α␈↓↓what␈↓␈αto␈αprint.␈α The␈αsimplest␈αthing␈αto␈αdo␈αis␈αto␈αstore␈αa
␈↓ ↓H␈↓representation␈α∞of␈α∞the␈α∞name␈α∞on␈α∞each␈α∞p-list.␈α∞ This␈α∞is␈α∞done␈α∞with␈α∞another␈α∞indicator␈α∂called␈α∞␈↓αPNAME␈↓,
␈↓ ↓H␈↓standing␈α∪for␈α∩␈↓↓print-name␈↓.␈α∪Each␈α∪atom␈α∩is␈α∪guaranteed␈α∩to␈α∪have␈α∪a␈α∩print-name␈α∪or␈α∪"p-name".␈α∩ The
␈↓ ↓H␈↓print-name of the atom is what the LISP output routine will print as the name of the atom.

␈↓ ↓H␈↓The atom ␈↓αBAZ␈↓ will have at least the following:
␈↓"␈↓ ↓H␈↓
                ⊂αααααααπααααααα⊃
␈↓"␈↓ ↓H␈↓
                ~ PNAME ~ "BAZ" ~
␈↓"␈↓ ↓H␈↓
                %ααααααα∀ααααααα$

␈↓ ↓H␈↓Where ␈↓
"BAZ"␈↓ means a representation of the string of characters, ␈↓αB, A, Z␈↓.

␈↓ ↓H␈↓When␈α
we␈α
wish␈αto␈α
␈↓↓represent␈↓␈α
such␈αa␈α
property␈α
pair␈αwe␈α
must␈α
deal␈αwith␈α
representational␈α
problems␈αof
␈↓ ↓H␈↓character␈αstrings.␈α
 The␈αsalient␈αproperties␈α
here␈αare␈αthe␈α
desire␈αto␈αhave␈α
strings␈αof␈α
unbounded␈αlength,
␈↓ ↓H␈↓but␈α⊗represent␈α⊗them␈α⊗in␈α⊗a␈α⊗machine␈α⊗with␈α↔fixed␈α⊗word␈α⊗size.␈α⊗We␈α⊗will␈α⊗discuss␈α⊗a␈α↔more␈α⊗general
␈↓ ↓H␈↓representation␈α⊗in␈α⊗Section ,␈α⊗but␈α⊗here␈α⊗we␈α⊗will␈α⊗represent␈α⊗the␈α⊗print␈α⊗name␈α⊗by␈α⊗using␈α⊗the␈α∃basic
␈↓ ↓H␈↓dotted-pair data structure of LISP.
␈↓ ↓H␈↓%2204  static structure␈↓ _5.8%*



␈↓"␈↓ ↓H␈↓
                                 ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                                 ~ # ~≤'~
␈↓"␈↓ ↓H␈↓
                                 %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                                   ↓
␈↓"␈↓ ↓H␈↓
                                 ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
                                 ~ BAZ≡≡ ~
␈↓"␈↓ ↓H␈↓
                                 %ααααααα$


␈↓ ↓H␈↓␈↓ ∧z␈↓↓p-name representation for ␈↓αBAZ␈↓. 

␈↓ ↓H␈↓␈↓
BAZ≡≡␈↓␈αmeans␈αa␈αmemory␈αlocation␈αcontaining␈αsome␈αencoding␈αof␈αthe␈αletters␈α␈↓
B␈↓,␈α␈↓
A␈↓,␈αand␈α␈↓
Z␈↓.␈α The␈αsymbol,
␈↓ ↓H␈↓␈↓
≡␈↓,␈α
represents␈αsome␈α
non-printing␈α
character;␈αthus␈α
in␈α
the␈αabove␈α
diagram,␈α
we␈αare␈α
assuming␈α
a␈αlocation
␈↓ ↓H␈↓can contain five characters.

␈↓ ↓H␈↓We␈α∂represent␈α∞the␈α∂print-name␈α∂as␈α∞a␈α∂list␈α∂so␈α∞that␈α∂we␈α∞may␈α∂allow␈α∂atoms␈α∞with␈α∂p-names␈α∂of␈α∞unbounded
␈↓ ↓H␈↓length. Thus the p-name for ␈↓αBLETCH␈↓, would be:

␈↓"␈↓ ↓H␈↓
                 ⊂αααπααα⊃     ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                 ~ # ~ #αβαααα→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
                 %αβα∀ααα$     %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                   ↓             ↓
␈↓"␈↓ ↓H␈↓
                 ⊂ααααααα⊃     ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
                 ~ BLETC ~     ~ H≡≡≡≡ ~
␈↓"␈↓ ↓H␈↓
                 %ααααααα$     %ααααααα$


␈↓ ↓H␈↓␈↓ ∧{␈↓↓P-name structure for ␈↓αBLETCH␈↓. 

␈↓ ↓H␈↓With␈αsuch␈αprint-names␈α
on␈αeach␈αproperty-list␈α
␈↓αprint␈↓␈αcan␈αnow␈α
function:␈αit␈αwill␈α
search␈αthe␈αp-list␈αfor␈α
the
␈↓ ↓H␈↓indicator␈α
␈↓αPNAME␈↓␈α
and␈αprint␈α
what␈α
it␈α
finds.␈αFor␈α
the␈α
details␈α
of␈αLISP␈α
output␈α
see␈α
Section 5.10.␈α The
␈↓ ↓H␈↓␈↓αprint␈↓␈α
routine␈αneeds␈α
the␈αprint␈α
name␈αand,␈α
as␈αwe␈α
shall␈αsee␈α
shortly,␈αthe␈α
input␈αroutine␈α
needs␈α
the␈αprint
␈↓ ↓H␈↓name,␈α
but␈αotherwise␈α
all␈α
LISP␈αcalculation␈α
is␈α
done␈αusing␈α
the␈α
internal␈αpointers␈α
to␈α
the␈αrepresentation␈α
of
␈↓ ↓H␈↓the␈α
property␈α∞list.␈α
 Several␈α
implementations␈α∞exploit␈α
this␈α
observation␈α∞by␈α
placing␈α
the␈α∞print names␈α
in
␈↓ ↓H␈↓slower␈αmemory␈αthan␈αthat␈αused␈αfor␈αthe␈αmain␈αcomputation.␈αSince␈αaccess␈αto␈αprint␈αnames␈αis␈αinfrequent,
␈↓ ↓H␈↓we can afford to spend more time in retrieving them.

␈↓ ↓H␈↓Notice␈α∞that␈α∞the␈α∞actual␈α∞print-names␈α∞␈↓
BAZ≡≡␈↓,␈α∞␈↓
BLETC␈↓,␈α∞and␈α∞␈↓
H≡≡≡≡␈↓,␈α∞are␈α∞candidates␈α∞for␈α∞storage␈α∞in␈α
atom
␈↓ ↓H␈↓space␈αsince␈αthese␈αencodings␈αshould␈αnot␈αbe␈αinterpreted␈αas␈αpointers.␈α Since␈α"atom␈αspace"␈αis␈αno␈αlonger
␈↓ ↓H␈↓an␈α∞appropriate␈α
descriptor,␈α∞we␈α∞will␈α
give␈α∞it␈α∞a␈α
new␈α∞name.␈α∞ We␈α
will␈α∞call␈α∞that␈α
area␈α∞of␈α∞memory␈α
which
␈↓ ↓H␈↓contains information ␈↓↓not␈↓ to be interpreted as pointers, ␈↓↓Full Word Space␈↓, and abbreviate it as FWS.

␈↓ ↓H␈↓In␈αsummary,␈αwe␈αhave␈αdiscussed␈αthe␈αdetails␈αof␈αa␈αtypical␈αimplementation␈αof␈αthe␈αclass␈αof␈αS-exprs.␈αOur
␈↓ ↓H␈↓non-atomic␈α∞S-exprs␈α∂have␈α∞their␈α∂branching␈α∞structure␈α∂stored␈α∞in␈α∂what␈α∞we␈α∂have␈α∞called␈α∂pointer␈α∞space.
␈↓ ↓H␈↓Our␈α∪initial␈α∪discussion␈α∩of␈α∪atoms␈α∪supposed␈α∩a␈α∪particular␈α∪simple␈α∩representation:␈α∪simply␈α∪store␈α∩the
␈↓ ↓H␈↓encoding␈αof␈αthe␈αatom␈αin␈αa␈αmemory␈αlocation␈αfound␈αin␈αa␈αseparate␈αspace␈αwhich␈αwe␈αcalled␈αatom␈αspace.
␈↓ ↓H␈↓␈↓↓5.8␈↓ πKRepresentation of property-lists     205␈↓


␈↓ ↓H␈↓Upon␈α→further␈α→reflection␈α→we␈α_decided␈α→that␈α→atoms␈α→should␈α→play␈α_a␈α→more␈α→active␈α→role␈α→in␈α_the
␈↓ ↓H␈↓implementation.␈αSince␈α
identifiers␈αare␈α
to␈αbe␈α
represented␈αas␈αatoms␈α
we␈αneeded␈α
some␈αway␈α
to␈αrepresent
␈↓ ↓H␈↓those␈α∞properties␈α∞typically␈α∞associated␈α∞with␈α∞identifiers.␈α
Identifiers␈α∞in␈α∞LISP␈α∞are,␈α∞among␈α∞other␈α
things,
␈↓ ↓H␈↓used␈α∞for␈α∞names␈α
of␈α∞functions␈α∞and␈α
names␈α∞of␈α∞variables.␈α
Thus␈α∞we␈α∞needed␈α
the␈α∞ability␈α∞to␈α∞represent␈α
at
␈↓ ↓H␈↓least␈α∪these␈α∪two␈α∩kinds␈α∪of␈α∪"values"␈α∪with␈α∩a␈α∪LISP␈α∪atom.␈α∪ We␈α∩introduced␈α∪the␈α∪general␈α∪scheme␈α∩of
␈↓ ↓H␈↓property-lists␈α∞and␈α
associated␈α∞such␈α
a␈α∞p-list␈α
with␈α∞each␈α
LISP␈α∞atom.␈α
All␈α∞the␈α
things␈α∞we␈α
know␈α∞about␈α
a
␈↓ ↓H␈↓specific␈αatom␈α
are␈αstored␈αon␈α
the␈αp-list.␈α
Thus␈αwe␈αwant␈α
each␈αatom␈αstored␈α
uniquely;␈αthen␈α
anyone␈αwho
␈↓ ↓H␈↓wants␈α
to␈α∞examine␈α
the␈α∞properties␈α
of␈α∞an␈α
atom␈α∞has␈α
only␈α∞to␈α
look␈α∞at␈α
the␈α∞p-list.␈α
Since␈α∞all␈α
of␈α∞our␈α
LISP
␈↓ ↓H␈↓programs␈α
must␈α
be␈α
read␈α∞into␈α
the␈α
memory␈α
we␈α∞let␈α
the␈α
input␈α
function␈α∞keep␈α
track␈α
of␈α
the␈α∞details.␈α
 On
␈↓ ↓H␈↓reading␈α∞a␈α∞literal␈α∞atom,␈α∞the␈α∂program␈α∞checks␈α∞the␈α∞current␈α∞table␈α∂of␈α∞atoms.␈α∞ If␈α∞the␈α∞atom␈α∂appears,␈α∞the
␈↓ ↓H␈↓program␈αreturns␈αa␈αpointer␈αto␈αthe␈αentry.␈α If␈αthe␈α
atom␈αdoes␈αnot␈αappear␈αit␈αconstructs␈αa␈αnew␈αtable␈α
entry
␈↓ ↓H␈↓consisting␈α∩of␈α⊃the␈α∩print␈α⊃name.␈α∩ The␈α⊃effect␈α∩of␈α⊃storing␈α∩atoms␈α⊃uniquely␈α∩was␈α⊃to␈α∩turn␈α∩our␈α⊃abstract
␈↓ ↓H␈↓LISP-trees␈αinto␈αlist␈αstructure.␈α Indeed,␈αthe␈αrepresentation␈αof␈αa␈αLISP␈αexpression␈αis␈αa␈αcomplex␈αnet␈αof
␈↓ ↓H␈↓pointers;␈αeven␈αatoms␈αare␈αnow␈αpointers.␈αThe␈αonly␈αLISP␈αobjects␈αwe␈αhave␈αrepresented␈αwhich␈α
are␈α␈↓↓not␈↓
␈↓ ↓H␈↓pointers are the actual print-names like ␈↓
BAZ≡≡␈↓.

␈↓ ↓H␈↓To␈α∞reinforce␈α∞our␈α∞discussion␈α∞we␈α∞illustrate␈α∞the␈α∞abstract␈α∞picture␈α∞of␈α∞␈↓αNIL␈↓␈α∞and,␈α∞on␈α∞the␈α∞next␈α∞page,␈α
the
␈↓ ↓H␈↓representation␈αof␈αthe␈αatom␈α␈↓αNIL␈↓.␈αIn␈αall␈αof␈αthe␈αresulting␈αworms␈αthere␈αare␈αonly␈αthree␈αelements␈αin␈αFull
␈↓ ↓H␈↓Word Space; everything else is a pointer.

␈↓"␈↓ ↓H␈↓
         ⊂αααααα→αααααα→ααααααα→αααααα→ααααααα⊃
␈↓"␈↓ ↓H␈↓
         ↑                                    ↓
␈↓"␈↓ ↓H␈↓
   ⊂ααα←ααααα←ααααα⊃                          ~
␈↓"␈↓ ↓H␈↓
   ↓     ↑         ~     ⊂α←α⊃                ↓
␈↓"␈↓ ↓H␈↓
   ~   ⊂αβαπααααα⊃ ↑     ↓ ⊂αβαπααααααα⊃    ⊂αααπααααααα⊃
␈↓"␈↓ ↓H␈↓
   %αα→~ # ~ #α→αβ→$     ε→~ # ~ PNAME ~    ~ # ~ CONST ~
␈↓"␈↓ ↓H␈↓
       εαααβαααααλ       ↑ %ααα∀ααααααα$    %αβα∀ααααααα$
␈↓"␈↓ ↓H␈↓
       ~ # ~ NIL ~       %←αααααα←αααααα←ααααα$
␈↓"␈↓ ↓H␈↓
       %αβα∀ααααα$       ↑
␈↓"␈↓ ↓H␈↓
         %→αααααα→αααααα→$
␈↓ ↓H␈↓␈↓↓206  static structure␈↓ 45.9␈↓


␈↓ ↓H␈↓␈↓ ∧}␈↓↓5.9  A picture of the atom ␈↓αNIL␈↓↓␈↓α


␈↓ ↓H␈↓We have been writing the atom ␈↓αNIL␈↓ as:
␈↓"␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
~∃~ #αβα→~ CONST ~ #αβα→~ # ~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
%α∀ααα$  %ααααααα∀ααα$  %αβα∀ααα$  %ααααααα∀ααα$  %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                          ↓                         ~  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                         NIL                        %α→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
                                                       %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                                                         ↓
␈↓"␈↓ ↓H␈↓
                                                      ⊂ααααα⊃
␈↓"␈↓ ↓H␈↓
                                                      ~NIL≡≡~
␈↓"␈↓ ↓H␈↓
                                                      %ααααα$

␈↓ ↓H␈↓where the atoms for ␈↓αPNAME␈↓ and ␈↓αCONST␈↓ are represented as:
␈↓"␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃        ⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
~∃~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~        ~∃~ #αβα→~ PNAME ~ #αβα→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
%α∀ααα$  %ααααααα∀ααα$  %αβα∀αα$        %α∀ααα$  %ααααααα∀ααα$  %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                          ↓                                       ↓
␈↓"␈↓ ↓H␈↓
                         ⊂αααπαα⊃                                ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                         ~ # ~≤'~                                ~ # ~≤'~
␈↓"␈↓ ↓H␈↓
                         %αβα∀αα$                                %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                           ↓                                       ↓
␈↓"␈↓ ↓H␈↓
                         ⊂ααααα⊃                                 ⊂ααααα⊃
␈↓"␈↓ ↓H␈↓
                         ~PNAME~                                 ~CONST~
␈↓"␈↓ ↓H␈↓
                         %ααααα$                                 %ααααα$


␈↓ ↓H␈↓In full detail, ␈↓αNIL␈↓ is represented as:
␈↓"␈↓ ↓H␈↓
 ⊂ααααααααααααααααααα←ααααααααααααααααααααααααααα←ααααααπααααπααααααααπααα⊃
␈↓"␈↓ ↓H␈↓
 ↓                                                      ↑    ~        ↑   ↑
␈↓"␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπααα⊃  ⊂αααααααπααα⊃  ⊂αααπαβα⊃  ↑        ~   ~
␈↓"␈↓ ↓H␈↓
~∃~ #αβα→~   #   ~ #αβα→~ # ~ #αβα→~   #   ~ #αβα→~ # ~ # ~  ~        ~   ~
␈↓"␈↓ ↓H␈↓
%α∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$  ~        ~   ~
␈↓"␈↓ ↓H␈↓
 ↑           %αααααααααα→ ~ →αααααααα→ ~→αα⊃        ~        ↑        ~   ~
␈↓"␈↓ ↓H␈↓
 ~                        ↓            ~   ~        ~  ⊂αααπαβα⊃      ~   ~
␈↓"␈↓ ↓H␈↓
 ~                        ~            ↓   ↓        %α→~ # ~ # ~      ~   ~
␈↓"␈↓ ↓H␈↓
 ~                        ↓            ~   ~           %αβα∀ααα$      ~   ~
␈↓"␈↓ ↓H␈↓
 ~                        ~            ~   ~             ↓            ↑   ↑
␈↓"␈↓ ↓H␈↓
 %ααααααα←ααααααααα←αααααα∀αααπαααα⊃   ~   ~          ⊂ααααα⊃         ~   ~
␈↓"␈↓ ↓H␈↓
                              ↑    ↑   ~   ~          ~NIL≡≡~         ~   ~
␈↓"␈↓ ↓H␈↓
                              ~    ~   ↓   ~          %ααααα$         ~   ~
␈↓"␈↓ ↓H␈↓
 ⊂αααααααααα←ααααααααααααααα← ~ ←α ~ ←α$   ~                          ~   ~
␈↓"␈↓ ↓H␈↓
 ↓                            ~    ~       ↓                          ~   ~
␈↓"␈↓ ↓H␈↓
⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαβα⊃  ~    ⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπαβα⊃ ↑
␈↓"␈↓ ↓H␈↓
~∃~ #αβα→~   #   ~ #αβα→~ # ~ # ~  ↑    ~∃~ #αβα→~   #   ~ #αβα→~ # ~ # ~ ~
␈↓"␈↓ ↓H␈↓
%α∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$  ~    %α∀ααα$  %αααβααα∀ααα$  %αβα∀ααα$ ~
␈↓"␈↓ ↓H␈↓
 ↑           ~            ↓        ~                 ~            ↓       ~
␈↓"␈↓ ↓H␈↓
 εααααα←ααααα$           ⊂αααπααα⊃ ↑                 ↓           ⊂αααπααα⊃↑
␈↓"␈↓ ↓H␈↓
 ~                       ~ # ~ #αβα$                 ~           ~ # ~ #αβ$
␈↓"␈↓ ↓H␈↓
 ~                       %αβα∀ααα$                   ~           %αβα∀ααα$
␈↓"␈↓ ↓H␈↓
 ~                         ↓                         ~             ↓
␈↓"␈↓ ↓H␈↓
 ↑                       ⊂ααααα⊃                     ~           ⊂ααααα⊃
␈↓"␈↓ ↓H␈↓
 ~                       ~PNAME~                     ↓           ~CONST~
␈↓"␈↓ ↓H␈↓
 ~                       %ααααα$                     ~           %ααααα$
␈↓"␈↓ ↓H␈↓
 %αααααααα←ααααααααααααα←ααααααααααααα←αααααααααααααα$
␈↓ ↓H␈↓␈↓↓5.10␈↓ πrInput/Output: ␈↓αread␈↓↓ and ␈↓αprint␈↓↓     207␈↓α


␈↓ ↓H␈↓␈↓ ∧j␈↓↓5.10  Input/Output: ␈↓αread␈↓↓ and ␈↓αprint␈↓↓␈↓α


␈↓ ↓H␈↓When␈α∞you␈α∞begin␈α∞to␈α∞implement␈α∞LISP␈α∞you␈α∞find␈α∞that␈α∞a␈α∞very␈α∞large␈α∞part␈α∞of␈α∞LISP␈α∞can␈α∞be␈α∞written␈α∞in
␈↓ ↓H␈↓LISP.  We have already seen that the evaluation process is expressible this way.

␈↓ ↓H␈↓Here␈α
we␈αwill␈α
see␈αthat␈α
the␈αmajority␈α
of␈αthe␈α
I/O␈αroutines␈α
can␈αbe␈α
written␈αas␈α
LISP␈αfunctions␈α
calling␈αa
␈↓ ↓H␈↓very␈αfew␈αprimitive␈αroutines.␈α The␈αprimitive␈αroutines␈αcan␈αalso␈αbe␈αdescribed␈αin␈α`pseudo-LISP'.␈α That
␈↓ ↓H␈↓is,␈αwe␈αwill␈αgive␈αa␈αLISP-like␈αdescription␈αof␈αthem,␈αthough␈αthey␈αwould␈αnormally␈αbe␈αcoded␈αin␈αmachine
␈↓ ↓H␈↓language.

␈↓ ↓H␈↓The primitive functions are ␈↓αratom␈↓ and ␈↓αprin1␈↓.

␈↓ ↓H␈↓␈↓αratom[␈α∂]␈↓␈α∞is␈α∂a␈α∂function␈α∞of␈α∂no␈α∂arguments.␈α∞It␈α∂reads␈α∂the␈α∞input␈α∂string,␈α∂constructing␈α∞the␈α∂next␈α∂atom␈α∞or
␈↓ ↓H␈↓␈↓ α8special␈αcharacter␈α(left␈αparen,␈αright␈αparen␈αor␈αdot).␈α It␈αlooks␈αup␈αthat␈αobject␈αin␈αthe␈αatom␈αtable
␈↓ ↓H␈↓␈↓ α8and␈α∂returns␈α∂a␈α∞pointer␈α∂to␈α∂that␈α∞table␈α∂entry.␈α∂ If␈α∞no␈α∂entry␈α∂is␈α∞found␈α∂an␈α∂appropriate␈α∂entry␈α∞is
␈↓ ↓H␈↓␈↓ α8made.␈α∞ ␈↓αratom␈↓␈α∞flushes␈α∂spaces␈α∞and␈α∞commas,␈α∞recognizing␈α∂them␈α∞as␈α∞delimiters.␈α∞It␈α∂returns␈α∞only
␈↓ ↓H␈↓␈↓ α8atoms or characters special to ␈↓αread␈↓.

␈↓ ↓H␈↓␈↓αpatom[x]␈↓␈αis␈αa␈αfunction␈αof␈αone␈αargument␈αexpecting␈αan␈αatom,␈αleft␈αparen,␈αright␈αparen,␈αblank,␈αor␈αdot␈αas
␈↓ ↓H␈↓␈↓ α8the value of its argument.  It will print the p-name of that object on the output device.

␈↓ ↓H␈↓␈↓αratom␈↓␈α
is␈α
the␈αLISP␈α
␈↓↓scanner␈↓.␈α
A␈αscanner␈α
is␈α
a␈α
basic␈αpart␈α
of␈α
an␈αinput␈α
processor.␈α
Typically␈α
a␈αscanner's
␈↓ ↓H␈↓job␈α
is␈αto␈α
negotiate␈αwith␈α
the␈αactual␈α
input␈αdevice␈α
for␈αinput␈α
characters.␈αThe␈α
scanner␈αbuilds␈α
the␈αmost
␈↓ ↓H␈↓basic␈α∂ingredients,␈α∂like␈α∂identifiers␈α∂from␈α∂alphanumeric␈α∂strings,␈α∂or␈α∂numbers␈α∂from␈α∂digit␈α⊂strings,␈α∂and
␈↓ ↓H␈↓only␈α∞after␈α∞such␈α∞a␈α∞basic␈α
block␈α∞has␈α∞been␈α∞recognized␈α∞is␈α
the␈α∞next␈α∞level␈α∞of␈α∞syntax␈α∞analysis␈α
attempted.
␈↓ ↓H␈↓The units, also called tokens, which the scanner has built are passed to the ␈↓↓parser␈↓.

␈↓ ↓H␈↓The␈αtypical␈αparser␈αbuilds␈αa␈αtree-representation␈αof␈αthe␈αinput␈αstring;␈αthe␈αstructure␈αof␈αthe␈αtree␈αreflects
␈↓ ↓H␈↓the␈α∂language␈α∞constructs␈α∂which␈α∂were␈α∞present␈α∂in␈α∂the␈α∞input.␈α∂ A␈α∂well-designed␈α∞parser␈α∂uses␈α∂the␈α∞BNF
␈↓ ↓H␈↓equations␈α∞which␈α∞describe␈α∞the␈α
class␈α∞of␈α∞well-formed␈α∞input␈α
expressions␈α∞to␈α∞determine␈α∞whether␈α∞or␈α
not
␈↓ ↓H␈↓the␈α∃input␈α∃stream␈α∀is␈α∃a␈α∃legal␈α∀expression.␈α∃ ␈↓αread␈↓␈α∃is␈α∀the␈α∃LISP␈α∃parser.␈α∀␈↓αread␈↓␈α∃will␈α∃recognize␈α∀both
␈↓ ↓H␈↓S-expression␈αand␈αlist-notation;␈αbesides␈αanalyzing␈αthe␈αinput␈αstream,␈α␈↓αread␈↓␈αalso␈αbuilds␈αan␈αS-expression
␈↓ ↓H␈↓representation of the input.

␈↓ ↓H␈↓To␈αmake␈αlife␈α
simpler␈αwe␈αneed␈α
to␈αbe␈αable␈α
to␈αrefer␈αto␈α
atoms␈αwhose␈αprint-names␈α
are␈αthe␈αcharacters␈α
"␈↓α)␈↓",
␈↓ ↓H␈↓"␈↓α(␈↓",␈α
".",␈αand␈α
"␈α
" (blank).␈α We␈α
will␈αassume␈α
that␈α
␈↓αRPAR␈↓,␈α␈↓αLPAR␈↓,␈α
␈↓αPERIOD␈↓,␈αand␈α
␈↓αBLANK␈↓␈α
denote␈αsuch
␈↓ ↓H␈↓atoms.  For example, if the next input character is "(" then

␈↓ ↓H␈↓␈↓ α@␈↓αeq[ratom[ ];LPAR]␈↓ is true (and the input pointer is moved to the next character!). 

␈↓ ↓H␈↓␈↓αpatom[PERIOD]␈↓ will have the effect of printing a ␈↓↓"."␈↓ on the output device.

␈↓ ↓H␈↓We will discuss the structure of ␈↓αratom␈↓ and ␈↓αpatom␈↓ in a moment.  In the meantime here is ␈↓αread␈↓:
␈↓ ↓H␈↓␈↓↓208  static structure␈↓ (5.10␈↓


␈↓ ↓H␈↓αread <=λ[[ ]prog[[j]
␈↓ ↓H␈↓α␈↓ βHj ← ratom[ ];
␈↓ ↓H␈↓α␈↓ βH[atom[j] →return[j];
␈↓ ↓H␈↓α␈↓ βH is_lpar[j] → return[read_head[ ]];
␈↓ ↓H␈↓α␈↓ βH is_dot[j] → err[ ];
␈↓ ↓H␈↓α␈↓ βH is_rpar[j] → err[ ] ]]]

␈↓ ↓H␈↓␈↓αread␈↓␈α
will␈α
return␈α
a␈α
representation␈α
of␈α
a␈α
legal␈α
S-expr␈α
or␈α
list,␈α
␈↓λα␈↓.␈α
 ␈↓αerr␈↓␈α
is␈α
a␈α
LISP␈α
system␈α
function␈α
which,␈α
in
␈↓ ↓H␈↓this␈α∀case,␈α∃will␈α∀terminate␈α∀the␈α∃input␈α∀scanning␈α∀immediately.␈α∃ ␈↓αread_head␈↓␈α∀will␈α∀translate␈α∃strings␈α∀␈↓λα␈↓
␈↓ ↓H␈↓acceptable␈αin␈αthe␈αcontext␈α"␈↓α(␈↓λα␈↓".␈α Thus␈α␈↓λα␈↓␈αbeing␈α␈↓α"A)"␈↓␈αor␈α␈↓α"A (B . C))"␈↓␈αwould␈αbe␈αsuitable␈αfor␈α␈↓αread_head␈↓;
␈↓ ↓H␈↓␈↓α(A)␈↓␈α
and␈α␈↓α(A (B . C))␈↓␈α
are␈α
S-exprs␈αor␈α
lists.␈α ␈↓α. A)␈↓␈α
would␈α
not␈αbe␈α
acceptable␈αsince␈α
␈↓α(. A)␈↓␈α
is␈αnot␈α
an␈αS-expr␈α
or
␈↓ ↓H␈↓list.

␈↓ ↓H␈↓αread_head <= λ[[ ]prog[[j]
␈↓ ↓H␈↓α␈↓ βH␈↓ βxj ← ratom[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx[atom[j] → return[cons[j;read_tail[]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_lpar[j] → return[cons[read_head[ ];read_tail[ ]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_dot[j] → err[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_rpar[j] → return[NIL] ]]]


␈↓ ↓H␈↓␈↓αread_head␈↓ is looking for legal ␈↓λα␈↓'s in the context "␈↓α(␈↓λα␈↓".  If it sees:
␈↓ ↓H␈↓␈↓ βHan atom, then ␈↓λα␈↓ is <atom>␈↓λβ␈↓α)␈↓;
␈↓ ↓H␈↓␈↓ βHa left parenthesis, then ␈↓λα␈↓ is ␈↓α(␈↓λβ␈↓α)␈↓λ∂␈↓α)␈↓;
␈↓ ↓H␈↓␈↓ βHa dot, then ␈↓λα␈↓ is ␈↓α.␈↓λβ␈↓α)␈↓;
␈↓ ↓H␈↓␈↓ βHa right parenthesis, then ␈↓λα␈↓ is ␈↓α)␈↓.

␈↓ ↓H␈↓αread_tail <= λ[[ ]prog[[j]
␈↓ ↓H␈↓α␈↓ βH␈↓ βxj ← ratom[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx[atom[j] → return[cons[j;read_tail[]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_lpar[j] → return[cons[read_head[ ];read_tail[ ]]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_dot[j] → return[read_cdr[]];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx is_rpar[j] → return[NIL] ]]]


␈↓ ↓H␈↓␈↓αread_tail␈↓␈αis␈αlooking␈αfor␈αlegal␈α␈↓λα␈↓'s␈αin␈αthe␈αcontext␈α"␈↓α(␈↓<sexpr> ␈↓λα␈↓".␈α The␈αstructure␈αof␈αthis␈αfunction␈αis␈αthat
␈↓ ↓H␈↓of␈α␈↓αread_head␈↓␈αexcept␈αfor␈αrecognition␈αof␈αdots.␈α"␈↓α.␈↓λβ␈↓α)␈↓"␈α␈↓↓is␈↓␈αplausible␈αin␈αthe␈αcontext␈α"␈↓α(␈↓<sexpr> ␈↓λα␈↓".␈α It␈αis␈αup
␈↓ ↓H␈↓to ␈↓αread_cdr␈↓ to see if its expectations are fulfilled.

␈↓ ↓H␈↓αread_cdr <= λ[[ ] prog[[j]
␈↓ ↓H␈↓α␈↓ βH␈↓ βxj ← read[ ];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx[is_rpar[ratom[ ]] → return [j];
␈↓ ↓H␈↓α␈↓ βH␈↓ βx ␈↓
t␈↓α → err[ ] ]]]

␈↓ ↓H␈↓That is, the only input legal after a dot is a S-expr or list followed by a right parenthesis.
␈↓ ↓H␈↓␈↓↓5.10␈↓ πrInput/Output: ␈↓αread␈↓↓ and ␈↓αprint␈↓↓     209␈↓α


␈↓ ↓H␈↓α␈↓ β0is_dot[x] <= eq[x;PERIOD]     is_lpar[x] <= eq[x;LPAR],  ...␈↓ etc.

␈↓ ↓H␈↓Here are ␈↓αprint␈↓ and friends. ␈↓αterpri␈↓ initiates a new output line.  (note: the value of ␈↓αprint[x]␈↓ is ␈↓αx␈↓.)

␈↓ ↓H␈↓αprint <= λ[[x]prog[[ ]
␈↓ ↓H␈↓α␈↓ αXprin0[x];
␈↓ ↓H␈↓α␈↓ αXterpri[ ];
␈↓ ↓H␈↓α␈↓ αXreturn[x]]]

␈↓ ↓H␈↓αprin0 <= λ[[x]prog[[j]
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ[atom[x] → return[patom[x]]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλj ← x;
␈↓ ↓H␈↓α␈↓ αX␈↓ βλpatom[LPAR];
␈↓ ↓H␈↓α␈↓ αXa3␈↓ βλprin0[car[j]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ[null[cdr[j]] →␈↓ ∧Hpatom[RPAR];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓ ∧Hreturn[x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλpatom[BLANK];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ[atom[cdr[j]] →␈↓ ∧Hpatom[PERIOD];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓ ∧Hpatom[BLANK];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓ ∧Hpatom[cdr[j]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓ ∧Hpatom[RPAR];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλ␈↓ ∧Hreturn[x]];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλj ← cdr[j];
␈↓ ↓H␈↓α␈↓ αX␈↓ βλgo[a3] ]]

␈↓ ↓H␈↓Notice that we have used the extended conditional expression as described in Section 6.1␈↓π 106␈↓.

␈↓ ↓H␈↓The␈α
basic␈α
␈↓αprint␈↓␈α
routine␈α
allows␈α
us␈α
to␈α
print␈α
data␈α
structures␈α
and␈α
program␈α
representations;␈α
however␈α
the
␈↓ ↓H␈↓format of such output is a simple linear string of atoms, numbers, spaces, and parens.

␈↓ ↓H␈↓For␈αexample␈αa␈α
␈↓αprint␈↓-based␈αprogram␈αfor␈α
printing␈αfunction␈αdefinitions␈α
might␈αprint␈αthe␈α
following␈αas
␈↓ ↓H␈↓the definition of ␈↓αmember␈↓:

␈↓ ↓H␈↓α␈↓ ∧λ(MEMBER␈α⊂(LAMBDA␈α⊂(X␈α⊂L)␈α⊂(COND␈α⊂((NULL
␈↓ ↓H␈↓α␈↓ ∧λL)␈α
NIL)␈α((EQ␈α
X␈α
(FIRST␈αL))␈α
T)␈α
(T␈α(MEMBER
␈↓ ↓H␈↓α␈↓ ∧λX (REST L))))))


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 106␈↓␈αNotice␈αtoo␈αthat␈α␈↓αprint[(A .(B . C))]␈↓␈αprints␈αas␈α␈↓α(A B . C)␈↓.␈αThis␈αis␈αbecause␈α␈↓αprint␈↓␈αdoesn't␈α
know␈αthat
␈↓ ↓H␈↓the␈α∞structure␈α∞is␈α∞not␈α∞a␈α∞list␈α∞until␈α∞it␈α∂sees␈α∞the␈α∞last␈α∞dotted-pair.␈α∞There␈α∞are␈α∞two␈α∞ways␈α∞of␈α∂handling␈α∞this:
␈↓ ↓H␈↓either␈α
require␈αa␈α
type-code,␈αtelling␈α
whether␈αthe␈α
structure␈αis␈α
a␈αdotted␈α
pair␈αor␈α
a␈αlist,␈α
represented␈α
as␈αa
␈↓ ↓H␈↓dotted␈α⊂pair.␈α⊂Then␈α⊂␈↓↓all␈↓␈α⊂dotted␈α∂pairs␈α⊂are␈α⊂printed␈α⊂in␈α⊂dot␈α⊂notation,␈α∂and␈α⊂␈↓↓all␈↓␈α⊂lists␈α⊂are␈α⊂printed␈α⊂in␈α∂list
␈↓ ↓H␈↓notation.␈α
The␈α
other␈α
alternative␈α
is␈α
to␈α
first␈α
examine␈α
the␈α
structure;␈α
if␈α
it␈α
is␈α
a␈α
list␈α∞representation,␈α
then
␈↓ ↓H␈↓print␈αit␈αthat␈αway,␈αotherwise␈αprint␈αit␈αas␈αa␈αdotted␈αpair.␈αThis␈αproblem␈αis␈αanother␈αindication␈αof␈α"object
␈↓ ↓H␈↓vs. representation".
␈↓ ↓H␈↓␈↓↓210  static structure␈↓ (5.10␈↓


␈↓ ↓H␈↓The␈α
print␈α
routine␈αcan␈α
break␈α
the␈α
text␈αat␈α
the␈α
end␈αof␈α
any␈α
atom␈↓π 107␈↓;␈α
the␈αonly␈α
restriction␈α
we␈α
place␈αon
␈↓ ↓H␈↓printing of expressions is that what is ␈↓αprint␈↓-ed must be ␈↓αread␈↓-able.

␈↓ ↓H␈↓Even␈α
with␈αa␈α
small␈α
definition␈αlike␈α
this,␈α
we␈αhave␈α
difficulty␈α
deciphering␈αthe␈α
structure.␈αWhen␈α
functions
␈↓ ↓H␈↓or␈α≥lists␈α≤become␈α≥large␈α≤and␈α≥deeply␈α≤nested␈α≥the␈α≤readability␈α≥becomes␈α≥unmanageable.␈α≤Most
␈↓ ↓H␈↓implementations␈α
of␈α
LISP␈α
supply␈α
formatting␈α
programs␈α
called␈α
"pretty-printers"␈α
or␈α
"grinders"␈α
to␈α
supply
␈↓ ↓H␈↓indenting to the basic print routine.

␈↓ ↓H␈↓Thus a pretty-printer might print ␈↓αmember␈↓ as:

␈↓ ↓H␈↓α(MEMBER
␈↓ ↓H␈↓α (LAMBDA (X L)
␈↓ ↓H␈↓α  (COND␈↓ αX((NULL L) NIL)
␈↓ ↓H␈↓α␈↓ αX((EQ X (FIRST L)) T)
␈↓ ↓H␈↓α␈↓ αX(T (MEMBER X (REST L))))))

␈↓ ↓H␈↓See Section 6.2 for a more detailed description of such functions.

␈↓ ↓H␈↓So␈α∩far␈α∩we␈α∪have␈α∩thrown␈α∩all␈α∪the␈α∩I/O␈α∩back␈α∩on␈α∪␈↓αratom␈↓␈α∩and␈α∩␈↓αpatom␈↓.␈α∪ Clearly␈α∩␈↓αratom␈↓␈α∩will␈α∪be␈α∩more
␈↓ ↓H␈↓interesting.␈αAll␈α␈↓αpatom␈↓␈α
need␈αdo␈αis␈αget␈α
the␈αp-name␈αand␈α
print␈αit.␈α ␈↓αratom␈↓␈αneeds␈α
to␈αdo␈αan␈αefficient␈α
search
␈↓ ↓H␈↓of␈αthe␈αatom␈αtable␈αand␈αif␈αthe␈αatom␈αis␈αnot␈αfound,␈αadd␈αit␈αto␈αthe␈αtable.␈α All␈α␈↓αratom␈↓␈αhas␈αto␈αwork␈αwith␈αis
␈↓ ↓H␈↓the␈αactual␈α
character␈αstring␈α
which␈αwill␈α
be␈αthe␈α
p-name␈αof␈α
some␈αatom.␈α
 What␈α␈↓αratom␈↓␈α
could␈αdo␈α
is␈αlook␈α
at
␈↓ ↓H␈↓the␈αp-name␈αof␈αeach␈α
atom␈αcurrently␈αin␈αthe␈αtable␈α
of␈αatoms;␈αwhen␈αit␈αfinds␈α
a␈αmatch␈αit␈αreturns␈αa␈α
pointer
␈↓ ↓H␈↓to␈αthat␈αatom.␈αThis␈αis␈αessentially␈α
the␈αsearch␈αscheme␈αof␈α␈↓αassoc␈↓.␈α If␈α
the␈αappropriate␈αatom␈αis␈αnot␈αfound␈α
it
␈↓ ↓H␈↓can␈αbuild␈αa␈αnew␈αone␈αconsisting␈αof␈αthe␈αp-name,␈αadd␈αit␈αto␈αthe␈αtable,␈αand␈αreturn␈αa␈αpointer␈αto␈αthis␈αnew
␈↓ ↓H␈↓entry.  In the next section we will introduce an alternative scheme called hashing.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓␈↓↓1.␈↓␈αLet␈α␈↓αprin_fw[x]␈↓␈αbe␈αa␈αprimitive␈αprinting␈αfunction␈αwhere␈α
␈↓αx␈↓␈αis␈αto␈αbe␈αan␈αactual␈αelement␈αin␈αFull␈α
Word
␈↓ ↓H␈↓Space.␈α∂␈↓αprin_fw␈↓␈α∞is␈α∂to␈α∞print␈α∂the␈α∞character␈α∂string␈α∂on␈α∞the␈α∂output␈α∞device.␈α∂You␈α∞may␈α∂assume␈α∂it␈α∞knows
␈↓ ↓H␈↓about ␈↓
≡␈↓. Write ␈↓αpatom␈↓ using ␈↓αprin_fw␈↓.

␈↓ ↓H␈↓␈↓↓2.␈↓␈αYou␈αmight␈αhave␈αnoticed␈αthat␈αthe␈αdefinitions␈αof␈α␈↓αread_head␈↓␈αand␈α␈↓αread_tail␈↓␈αare␈αalmost␈αidentical:␈αthe
␈↓ ↓H␈↓difference␈α∞involves␈α∞treatment␈α∞of␈α∞dots.␈α∞ Write␈α
new␈α∞versions␈α∞of␈α∞these␈α∞functions␈α∞utilizing␈α∞a␈α
common
␈↓ ↓H␈↓routine and functional arguments.

␈↓ ↓H␈↓␈↓↓3.␈↓ Write the set of BNF equations that drive ␈↓αread␈↓.

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 107␈↓␈α⊂Some␈α⊂implementations␈α⊂even␈α⊂allow␈α⊂the␈α⊂printer␈α⊂to␈α⊂break␈α⊂in␈α⊂the␈α⊂middle␈α⊂of␈α⊂an␈α⊂atom.␈α⊂This␈α∂is
␈↓ ↓H␈↓accomplished␈αby␈αdesignating␈αa␈αspecial␈αcharacter␈αfor␈αcarriage␈αcontrol,␈αand␈αthe␈α␈↓αread␈↓␈αroutine␈αknows␈αto
␈↓ ↓H␈↓ignore the immediately following end-of-line sequence.
␈↓ ↓H␈↓␈↓↓5.11␈↓ λ!Table searching: Hashing     211␈↓


␈↓ ↓H␈↓␈↓ ¬β␈↓↓5.11  Table searching: Hashing␈↓


␈↓ ↓H␈↓Table␈α∞lookup␈α∞is␈α∞analogous␈α∞to␈α∞the␈α∞problem␈α∂of␈α∞looking␈α∞up␈α∞words␈α∞in␈α∞a␈α∞dictionary.␈α∞ The␈α∂scheme␈α∞of
␈↓ ↓H␈↓␈↓αassoc␈↓␈α∂is␈α∂analogous␈α∂to␈α∂a␈α∂person␈α∂beginning␈α∂at␈α∂page␈α∂one␈α∂of␈α∂the␈α∂dictionary␈α∂and␈α∂proceeding␈α∞linearly
␈↓ ↓H␈↓(page-by-page␈αand␈αword-by-word)␈αthrough␈αthe␈αbook␈αuntil␈αhe␈αfinds␈αthe␈αword␈αin␈αquestion.␈α What␈αwe
␈↓ ↓H␈↓normally␈αdo␈αis␈αlook␈αat␈αthe␈αfirst␈αcharacter␈αof␈αthe␈αword␈αand␈αgo␈αimmediately␈αto␈αthe␈αsubsection␈α
of␈αthe
␈↓ ↓H␈↓dictionary␈α
which␈α
has␈α
the␈α
words␈α
beginning␈α
with␈α
that␈α
character.␈α
 We␈α
know␈α
that␈α
if␈α
we␈α
cannot␈αfind␈α
the
␈↓ ↓H␈↓definition␈αof␈αour␈αword␈αin␈αthat␈αsubsection␈αwe␈αneed␈αlook␈αno␈αfurther␈αin␈αthe␈αbook.␈α Usually␈αwe␈αdelimit
␈↓ ↓H␈↓our␈αsearch␈αeven␈αfurther␈αby␈αkeying␈αon␈αsubsequent␈αcharacters␈αin␈αthe␈αword.␈α Finally␈αwe␈αmay␈αresort␈αto
␈↓ ↓H␈↓linear␈α∂search␈α∂to␈α∂pin␈α∂down␈α∂the␈α∂word␈α∂on␈α∂a␈α∂specific␈α∂page␈α∂or␈α∂column.␈α∂ What␈α∂we␈α∂want␈α∂is␈α∂a␈α∂similar
␈↓ ↓H␈↓scheme␈αfor␈αthe␈αmachine.␈α We␈αmight␈αin␈αfact␈αmimic␈αthe␈αdictionary␈αsearch,␈αsubdividing␈αour␈αtable␈αinto
␈↓ ↓H␈↓26 subsections.  We can do better.

␈↓ ↓H␈↓Since␈αit␈αis␈αthe␈αmachine␈αwhich␈α
will␈αsubdivide␈αand␈αindex␈αinto␈αthe␈α
table,␈αwe␈αcan␈αpick␈αa␈αscheme␈α
which
␈↓ ↓H␈↓is␈αcomputationally␈αconvenient␈αfor␈αthe␈αmachine.␈αBesides␈αbeing␈αconvenient,␈αthe␈αscheme␈αshould␈αresult
␈↓ ↓H␈↓in␈αrather␈αeven␈αdistribution␈αof␈αatoms␈αin␈αthe␈αsubsections.␈αIf␈αthe␈αmajority␈αof␈αthe␈αatoms␈αend␈αup␈αin␈αthe
␈↓ ↓H␈↓same partition of the table we will have gained little towards improving the search efficiency.

␈↓ ↓H␈↓Each␈αof␈αthe␈αpartitions␈αor␈αsubdivisions␈αof␈αthe␈αtable␈αis␈αcalled␈αa␈α␈↓↓bucket␈↓.␈α Each␈αatom␈αwill␈αappear␈αin␈αat
␈↓ ↓H␈↓most␈α∪one␈α∪bucket.␈α∪ The␈α∪computational␈α∪scheme␈α∩or␈α∪function␈α∪used␈α∪to␈α∪determine␈α∪which␈α∪bucket␈α∩a
␈↓ ↓H␈↓particular␈αatom␈αbelongs␈αin␈αis␈αcalled␈αa␈α␈↓↓hashing␈αfunction␈↓.␈α All␈α␈↓αratom␈↓␈αhas␈αto␈αwork␈αwith␈αis␈α␈↓αchr-str␈↓,␈αthe
␈↓ ↓H␈↓encoding␈α
of␈α
the␈αactual␈α
name␈α
of␈α
the␈αatom.␈α
 The␈α
hashing␈α
function␈αwill␈α
use␈α
␈↓αchr-str␈↓␈α
to␈αdetermine␈α
which
␈↓ ↓H␈↓bucket␈αto␈αexamine.␈α Given␈αthe␈αbucket␈αnumber,␈αwe␈αthen␈αrun␈αdown␈αthe␈αlist␈αof␈αatoms␈αin␈αthat␈αbucket,
␈↓ ↓H␈↓comparing␈αprint-names␈αagainst␈α␈↓αchr-str␈↓.␈α
 If␈αthe␈αatom␈αwith␈αprint-name␈α
␈↓αchr-str␈↓␈αdoes␈αnot␈αappear␈αin␈α
that
␈↓ ↓H␈↓bucket␈αwe␈αare␈αassured␈αthat␈αit␈αdoes␈αnot␈αappear␈αanywhere␈αin␈αthe␈αtable.␈α In␈αthis␈αcase␈αwe␈αcreate␈αa␈αnew
␈↓ ↓H␈↓atom structure and add it to that bucket.

␈↓ ↓H␈↓Here is a sample hashing function:

␈↓ ↓H␈↓␈↓↓1.␈↓ Assume that we have N+1 buckets, numbered 0, 1, 2 ... N.

␈↓ ↓H␈↓␈↓↓2.␈↓ Take the representation of ␈↓αchr-str␈↓ (it's a number) and divide that number by N+1.

␈↓ ↓H␈↓␈↓↓3.␈↓ Look at the remainder.  It's a number between 0 and N.

␈↓ ↓H␈↓␈↓↓4.␈↓ Use that remainder as the index to the appropriate bucket.

␈↓ ↓H␈↓The␈αLISP␈α
atom␈αtable,␈α
usually␈αcalled␈α
␈↓αOBLIST␈↓␈α(for␈α␈↓↓object␈α
list␈↓),␈αis␈α
a␈αlist␈α
of␈αbuckets.␈α
 Each␈αbucket␈αis␈α
a
␈↓ ↓H␈↓list␈α∂of␈α∂the␈α∂atoms␈α∂which␈α∂`hash'␈α∂to␈α∂that␈α∂bucket.␈α∂ We␈α∂actually␈α∂represent␈α∂the␈α∂object␈α∂list␈α∂as␈α∂an␈α∞array
␈↓ ↓H␈↓named␈α∪␈↓αoblist␈↓.␈α∪ Arrays␈α∩are␈α∪discussed␈α∪in␈α∪full␈α∩in␈α∪Section ,␈α∪but␈α∩basically␈α∪are␈α∪an␈α∪efficient␈α∩storage
␈↓ ↓H␈↓representation␈αfor␈αsequences␈αof␈αknown␈αlength.␈α
We␈αtypically␈αallocate␈αa␈αblock␈αof␈αsequential␈α
cells␈αand
␈↓ ↓H␈↓use␈α∂the␈α∂addressing␈α∂scheme␈α∂of␈α∂the␈α∂machine's␈α∂hardware␈α∂to␈α∂do␈α∂a␈α∂rapid␈α∂subscript␈α∂calculation.␈α∂ The
␈↓ ↓H␈↓hash␈αnumber␈αwill␈αgive␈αus␈αthe␈αarray␈αsubscript␈αand␈αwe␈αcan␈αgo␈αto␈αthe␈αright␈αbucket␈αimmediately.␈α We
␈↓ ↓H␈↓won't have to go ␈↓αcdr␈↓-ing down the object list to get to the bucket.
␈↓ ↓H␈↓%2212  static structure␈↓ 	5.11%*


␈↓"␈↓ ↓H␈↓
                        ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
  ⊂ααααααα←ααααααα←αααα←βα# ~ #αβα⊃
␈↓"␈↓ ↓H␈↓
  ~                     εαααβαααλ ↓
␈↓"␈↓ ↓H␈↓
  ~                     ~   ~   ~←$
␈↓"␈↓ ↓H␈↓
  ~        ⊂ααα←ααα←ααα←βα# ~ #αβ→⊃
␈↓"␈↓ ↓H␈↓
  ↓        ↓            εαααβαααλ ↓
␈↓"␈↓ ↓H␈↓
 ...    (to bucket 2)     ... ...
␈↓"␈↓ ↓H␈↓
  ↓        ↓            εαααβαααλ ↓
␈↓"␈↓ ↓H␈↓
  ~       ...       ⊂α←αβα# ~ ≤'~←$
␈↓"␈↓ ↓H␈↓
  ~                 ↓   εαααβααα$
␈↓"␈↓ ↓H␈↓
  ~                 %ααααααα→αααααα→αααααα⊃
␈↓"␈↓ ↓H␈↓
  ↓                                       ↓
␈↓"␈↓ ↓H␈↓
(to bucket 1)                           (to bucket n)
␈↓"␈↓ ↓H␈↓
  ~                                       ~
␈↓"␈↓ ↓H␈↓
  ~                                       ~  ⊂αααπααα⊃      ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
  ~ ⊂αααπααα⊃     ⊂αααπαα⊃                %α→~ # ~ #αβ→ ...→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
  %→~ # ~ #αβ→...→~ # ~≤'~                   %αβα∀ααα$      %αβα∀αα$
␈↓"␈↓ ↓H␈↓
    %αβα∀ααα$     %αβα∀αα$                     ↓              %→ ...
␈↓"␈↓ ↓H␈↓
      ~             ~                     ⊂απααα⊃
␈↓"␈↓ ↓H␈↓
  (to atom 1:      (atom m:               ~∃~ #αβ→(p-list of
␈↓"␈↓ ↓H␈↓
    bucket 1)        bucket 1)            %α∀ααα$     atom 1: bucket n)
␈↓"␈↓ ↓H␈↓
      ~             ↓
␈↓"␈↓ ↓H␈↓
      ↓          ⊂απααα⊃  ⊂αααααααπααα⊃  ⊂αααπααα⊃ ⊂ααααααπααα⊃ ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
     ...         ~∃~ #αβα→~ PNAME ~ #αβα→~ # ~ #αβ→~ SUBR ~ #αβ→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
                 %α∀ααα$  %ααααααα∀ααα$  %αβα∀ααα$ %αααααα∀ααα$ %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                                           ↓                      ↓
␈↓"␈↓ ↓H␈↓
                                         ⊂αααπαα⊃          to primitive code
␈↓"␈↓ ↓H␈↓
                                         ~ # ~≤'~             for ␈↓αcar␈↓

␈↓"␈↓ ↓H␈↓
                                         %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                                           ↓
␈↓"␈↓ ↓H␈↓
                                        ⊂ααααα⊃
␈↓"␈↓ ↓H␈↓
                                        ~CAR≡≡~
␈↓"␈↓ ↓H␈↓
                                        %ααααα$
␈↓ ↓H␈↓↓␈↓ βzPartial Object List; where atom m:bucket 1 is ␈↓αCAR␈↓↓

␈↓ ↓H␈↓Note:␈α
The␈αtop␈α
level␈α
of␈α␈↓αOBLIST␈↓␈α
is␈α
stored␈αsequentially␈α
for␈α
fast␈αaccess␈α
by␈α
the␈αhasher;␈α
the␈α␈↓αcdr␈↓-parts␈α
are
␈↓ ↓H␈↓chained␈α
together␈α
in␈α
a␈α
(sequential)␈α
list␈αso␈α
that␈α
the␈α
structure␈α
of␈α
the␈αtable␈α
will␈α
be␈α
like␈α
any␈α
other␈αlist.
␈↓ ↓H␈↓The chained representation is used by any other LISP process␈↓π 108␈↓.

␈↓ ↓H␈↓MacLISP␈α
embellishes␈αthe␈α
basic␈α
␈↓αOBLIST␈↓␈αin␈α
an␈α
important␈αway.␈α
That␈α
system␈αwill␈α
allow␈αseveral␈α
object
␈↓ ↓H␈↓lists␈αto␈αexist␈αwithin␈αthe␈αsystem.␈αThis␈αis␈αuseful␈αsince␈αseveral␈αcooperating␈αLISP␈αsubsystems␈αmay␈αexist;
␈↓ ↓H␈↓for␈αexample␈αthe␈αLISP␈αeditor,␈αdebugger,␈αand␈αcompiler␈αare␈αall␈αwritten␈αin␈αLISP␈αand␈αmay␈αall␈αbe␈αused
␈↓ ↓H␈↓within␈αthe␈αsame␈αinteractive␈αsession.␈α The␈αdifficulty␈αis␈αthat␈αeach␈αof␈αthose␈αsubsystems␈αmay␈αuse␈αnames
␈↓ ↓H␈↓which␈αconflict␈αwith␈αnames␈αin␈αthe␈αuser's␈αprograms.␈αMultiple␈αobject␈αlists␈αare␈αa␈αway␈αto␈α
overcome␈αthis
␈↓ ↓H␈↓problem.␈αOne␈αone␈αobject␈αlist␈αis␈αcurrent␈αat␈αany␈αtime,␈αbut␈αseveral␈αmay␈αexist.␈α There␈αare␈α
functions␈αto

␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 108␈↓␈α∂In␈α∂particular,␈α∞the␈α∂garbage␈α∂collector␈α∂uses␈α∞this␈α∂linking.␈α∂As␈α∞a␈α∂further␈α∂implementation␈α∂note,␈α∞the
␈↓ ↓H␈↓implementors␈α∞of␈α∞MacLISP␈α∞noted␈α∞the␈α∞frequent␈α∞use␈α
of␈α∞single␈α∞character␈α∞atoms␈α∞and␈α∞added␈α∞a␈α
special
␈↓ ↓H␈↓section␈αto␈αthe␈αtop-level␈αof␈αthe␈α
object␈αlist.␈αA␈αcontiguous␈αblock␈αof␈α
cells,␈αof␈αsize␈αequal␈αto␈αthe␈αnumber␈α
of
␈↓ ↓H␈↓characters,␈αwas␈αadded.␈αOn␈αreading␈αa␈αsingle␈αcharacter␈αatom,␈αthe␈αcorresponding␈αentry␈αin␈αthe␈αtable␈αis
␈↓ ↓H␈↓examined.␈αA␈α␈↓αNIL␈↓␈αsays␈αthe␈αatom␈α
hasn't␈αbeen␈αseen␈αbefore;␈αotherwise␈αits␈αp-list␈α
representation␈αresides
␈↓ ↓H␈↓there.
␈↓ ↓H␈↓␈↓↓5.11␈↓ λ!Table searching: Hashing     213␈↓


␈↓ ↓H␈↓create object lists, and object lists are swapped by λ-binding to the identifier ␈↓αobarray␈↓␈↓π 109␈↓.

␈↓ ↓H␈↓Consider:
␈↓ ↓H␈↓α␈↓ ¬`λ[[obarray]␈↓λx␈↓α][ob␈↓β1␈↓α].

␈↓ ↓H␈↓Assuming␈α∂that␈α∂␈↓αob␈↓β1␈↓␈α∂is␈α∂bound␈α∂to␈α∂an␈α∂object␈α⊂list,␈α∂then␈α∂within␈α∂the␈α∂evaluation␈α∂of␈α∂␈↓λx␈↓␈α∂the␈α⊂symbols␈α∂and
␈↓ ↓H␈↓bindings of ␈↓αob␈↓β1␈↓ would be accessible.

␈↓ ↓H␈↓Should␈αa␈α
linear␈αsearch␈α
and␈αstorage␈α
technique,␈αlike␈α␈↓αassoc-pairlis␈↓,␈α
or␈αa␈α
more␈αcomplex␈α
technique␈αlike
␈↓ ↓H␈↓hashing,␈αbe␈α
employed?␈α It␈αdepends␈α
on␈αthe␈αapplication,␈α
and␈αthe␈α
speed␈αand␈αsize␈α
of␈αthe␈αmachine.␈α
 The
␈↓ ↓H␈↓hash␈αtable␈αtakes␈αextra␈αspace␈αboth␈αfor␈αstorage␈αand␈αfor␈αprogram,␈αbut␈αgives␈αa␈αfaster␈αsearch␈αtime.␈αThe
␈↓ ↓H␈↓linear␈α
technique␈α
requires␈α∞less␈α
space,␈α
but␈α∞can␈α
be␈α
quite␈α
slow.␈α∞ ref␈α
[Ste pc],␈α
[Har 75].␈α∞ Several␈α
books
␈↓ ↓H␈↓cover searching and sorting in great detail ([Gri 71], [Knu xx]).

␈↓ ↓H␈↓Finally,␈α
we␈α
want␈α
to␈α
present␈αa␈α
version␈α
of␈α
␈↓αratom␈↓␈α
which␈αuses␈α
the␈α
organization␈α
we␈α
just␈α
described.␈α In
␈↓ ↓H␈↓this␈αdescription,␈αwe␈αwill␈αrestrict␈αourselves␈αto␈αrecognition␈αof␈αatoms,␈αleaving␈αthe␈αreader␈αto␈α
supply␈αthe
␈↓ ↓H␈↓necessary parts for recognition of numbers.  We will recognize three classes of characters:

␈↓ ↓H␈↓␈↓↓1.␈↓ The class of letters will include the alphabetic characters.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α∞The␈α∞class␈α∞of␈α∞delimiters␈α∞consists␈α∞of␈α∞those␈α∞characters␈α∞which␈α∞signal␈α∞the␈α∞end␈α∞of␈α∞an␈α∞atom.␈α∞For␈α∞this
␈↓ ↓H␈↓␈↓ ↓xscanner we assume space and carriage control are delimiters.

␈↓ ↓H␈↓␈↓↓3.␈↓ The special characters will consist of "(", ")" and "." .

␈↓ ↓H␈↓Special␈αcharacters␈αalso␈αact␈αas␈αdelimiters␈αin␈αLISP␈αand␈αthis␈αresults␈αin␈αa␈αslight␈αcomplication.␈αConsider
␈↓ ↓H␈↓the␈αpartial␈αstring␈α"␈↓αAB )C␈↓".␈αOur␈αscanner␈αshould␈αscan␈αthe␈α"␈↓αA␈↓",␈αscan␈αthe␈α"␈↓αB␈↓",␈αand␈αscanning␈αthe␈αspace,
␈↓ ↓H␈↓should␈α
recognize␈α
a␈α∞delimiter.␈α
It␈α
should␈α
recognize␈α∞the␈α
␈↓αAB␈↓␈α
as␈α
an␈α∞atom,␈α
and␈α
signal␈α
␈↓αread␈↓.␈α∞The␈α
string
␈↓ ↓H␈↓will␈α
be␈α
reduced␈α
to␈α
"␈↓α)C␈↓".␈α
The␈α
next␈α
time␈α␈↓αread␈↓␈α
calls␈α
␈↓αratom␈↓␈α
the␈α
right␈α
parenthesis␈α
will␈α
be␈αseen,␈α
recognized
␈↓ ↓H␈↓as a special character and an indication of that will be returned to ␈↓αread␈↓.

␈↓ ↓H␈↓Now␈αconsider␈αthe␈αstring␈α"␈↓αAB)C␈↓";␈α␈↓αratom␈↓␈αwill␈αscan␈α"␈↓αA␈↓"␈αand␈α"␈↓αB␈↓"␈αas␈αbefore.␈αIt␈αwill␈αthen␈αscan␈αthe␈α")".␈αIt
␈↓ ↓H␈↓now␈α⊂needs␈α⊂to␈α⊂do␈α⊂␈↓↓two␈↓␈α⊂things;␈α⊂it␈α⊂must␈α⊂signal␈α⊂␈↓αread␈↓␈α⊂about␈α⊂the␈α⊂atom␈α⊂it␈α⊂has␈α⊂seen,␈α⊂but␈α⊂it␈α⊃must␈α⊂also
␈↓ ↓H␈↓remember␈α
the␈α
")"␈α
so␈α
that␈α
the␈α
␈↓↓next␈↓␈α
time␈α
␈↓αread␈↓␈αasks␈α
for␈α
information,␈α
it␈α
sees␈α
the␈α
")"␈α
and␈α
not␈α
the␈α"␈↓αC␈↓".
␈↓ ↓H␈↓We␈αhandle␈α
this␈αproblem␈αby␈α
using␈αa␈αglobal␈α
variable␈αnamed␈α␈↓αlst_chr␈↓.␈α
 This␈αvariable␈αis␈α
initialized␈αto
␈↓ ↓H␈↓␈↓αNIL␈↓␈αand␈αremains␈αthat␈αway␈αuntil␈αour␈αanomalous␈αsituation␈αoccurs.␈αAt␈αthat␈αtime␈αthe␈αspecial␈αcharacter
␈↓ ↓H␈↓is␈αplaced␈αin␈α
␈↓αlst_chr␈↓,␈αand␈α␈↓αratom␈↓␈α
exits␈αnormally.␈αSo,␈α
whenever␈α␈↓αratom␈↓␈αis␈α
called,␈αthe␈αfirst␈α
thing␈αit␈αdoes␈α
is
␈↓ ↓H␈↓check␈α∞the␈α∞contents␈α∂of␈α∞␈↓αlst_chr␈↓.␈α∞ If␈α∂it␈α∞is␈α∞non-empty,␈α∞its␈α∂contents␈α∞is␈α∞returned,␈α∂as␈α∞␈↓αlst_chr␈↓␈α∞is␈α∂set␈α∞empty
␈↓ ↓H␈↓again.





␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 109␈↓ An object list is called object array in MacLISP.
␈↓ ↓H␈↓␈↓↓214  static structure␈↓ *5.11␈↓



␈↓ ↓H␈↓αratom <=λ[[]prog[[chr]
␈↓ ↓H␈↓α␈↓ β(␈↓ βH[lst_chr → swap[lst_chr;chr];return[chr]];
␈↓ ↓H␈↓α␈↓ β(a␈↓ βHchr ← readch[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH[is_let[chr] → stuf_buf[chr];ratom␈↓β1␈↓α[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH is_delim[chr] → go[a];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH is_spec[chr] → return[chr]]]]


␈↓ ↓H␈↓This␈αprocedure␈αuses␈αtricks␈αadvertised␈αin␈αSection 5.4,␈αusing␈α␈↓αlst_chr␈↓␈αas␈αa␈αpredicate,␈αand␈αknowing␈αthat
␈↓ ↓H␈↓␈↓αprog␈↓␈α∀variables␈α∀are␈α∀initialized␈α∀to␈α∀␈↓αNIL␈↓␈α∀and␈α∪that␈α∀the␈α∀representation␈α∀of␈α∀␈↓
f␈↓␈α∀is␈α∀␈↓αNIL␈↓.␈α∀ With␈α∪that
␈↓ ↓H␈↓knowledge, ␈↓αswap␈↓ swaps the contents of ␈↓αchr␈↓ and ␈↓αlst_chr␈↓.

␈↓ ↓H␈↓The␈αroutine␈α␈↓αreadch␈↓␈αgets␈αthe␈αnext␈αcharacter,␈αand␈α␈↓αstuf_buf␈↓␈αis␈αused␈αto␈αsave␈αthe␈αcharacter␈αstring␈αwhich
␈↓ ↓H␈↓is to become an atom.  The character string is built up in ␈↓αbuf␈↓.

␈↓ ↓H␈↓αratom␈↓β1␈↓α <= λ[[]prog[[chr]
␈↓ ↓H␈↓α␈↓ β(l␈↓ βHchr ← readch[];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH[is_delim[chr] → return[intern[buf]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH is_spec[chr] → lst_chr ← chr; return[intern[buf]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH ␈↓
t␈↓α → stufbuf[chr]; go[l]]]]

␈↓ ↓H␈↓If ␈↓αratom␈↓β1␈↓ sees a special character it is saved in ␈↓αlst_chr␈↓.

␈↓ ↓H␈↓αintern <= λ[[l][prog[chr-str]
␈↓ ↓H␈↓α␈↓ β(␈↓ βHchr-str ← maknam[buf];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHi ← hash[chr-str]
␈↓ ↓H␈↓α␈↓ β(␈↓ βHbucket ← oblist[i];
␈↓ ↓H␈↓α␈↓ β(a␈↓ βH[null[bucket] → return[insert[chr-str]]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHat ← get[first[bucket];PNAME];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH[right-one[at;chr_str] → return[first[bucket]]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHbucket ← rest[bucket];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHgo[a]]]



␈↓ ↓H␈↓␈↓αmaknam␈↓␈α⊂takes␈α⊂our␈α⊂character␈α⊂string␈α⊂and␈α⊂converts␈α⊂it␈α⊂into␈α⊂an␈α⊂appropriate␈α⊂numeric␈α⊂representation.
␈↓ ↓H␈↓␈↓αhash␈↓␈α
returns␈α
the␈α
bucket␈α
number␈α∞of␈α
its␈α
argument,␈α
and␈α
␈↓αinsert␈↓␈α
builds␈α∞the␈α
atom␈α
and␈α
inserts␈α
it␈α∞into␈α
a
␈↓ ↓H␈↓bucket.  ␈↓αright-one␈↓ is a predicate used to check if an atom has the right print-name.


␈↓ ↓H␈↓Typically␈α⊂an␈α⊃implementation␈α⊂of␈α⊃␈↓αratom␈↓␈α⊂will␈α⊃be␈α⊂done␈α⊃so␈α⊂that␈α⊃the␈α⊂class␈α⊃of␈α⊂special␈α⊃characters␈α⊂and
␈↓ ↓H␈↓delimiters␈α∂can␈α⊂be␈α∂varied.␈α⊂This␈α∂is␈α∂done␈α⊂using␈α∂a␈α⊂representation␈α∂of␈α∂a␈α⊂character␈α∂table␈α⊂whose␈α∂name
␈↓ ↓H␈↓entries␈α∂are␈α∂characters,␈α∂and␈α∞whose␈α∂value␈α∂entries␈α∂determine␈α∂the␈α∞␈↓αratom␈↓␈α∂properties.␈α∂ This␈α∂is␈α∂done␈α∞to
␈↓ ↓H␈↓allow␈α⊃LISP␈α⊃users␈α∩to␈α⊃define␈α⊃their␈α∩own␈α⊃parsers␈α⊃and␈α∩scanners.␈α⊃ LISP's␈α⊃modifiable␈α∩input␈α⊃routine,
␈↓ ↓H␈↓␈↓↓5.11␈↓ λ!Table searching: Hashing     215␈↓


␈↓ ↓H␈↓coupled␈αwith␈αits␈αdata␈αstructures␈αand␈αextendible␈αevaluator␈αmake␈αLISP␈αan␈αexcellent␈αtool␈αfor␈αbuilding
␈↓ ↓H␈↓more sophisticatd language systems.

␈↓ ↓H␈↓On␈α∂page 158␈α∂we␈α∂introduced␈α∂the␈α∂abbreviation␈α∞␈↓λ`␈↓αx␈↓␈α∂for␈α∂␈↓αquote[x]␈↓.␈α∂ Such␈α∂an␈α∂abbreviational␈α∂facility␈α∞is
␈↓ ↓H␈↓present␈α⊃in␈α⊃several␈α∩versions␈α⊃of␈α⊃LISP␈α∩and␈α⊃it␈α⊃is␈α∩the␈α⊃duty␈α⊃of␈α∩␈↓αratom␈↓␈α⊃to␈α⊃recognize␈α∩such␈α⊃constructs.
␈↓ ↓H␈↓Whenever␈α
␈↓αratom␈↓␈α∞sees␈α
the␈α∞prefix␈α
␈↓λ`␈↓␈α∞it␈α
reads␈α∞the␈α
next␈α
S-expr␈α∞␈↓λα␈↓␈α
and␈α∞returns␈α
the␈α∞list␈α
␈↓α(QUOTE␈α∞␈↓λα␈↓α)␈↓␈α
as
␈↓ ↓H␈↓value.␈α
This␈α
␈↓αquote␈↓␈α
facility␈α
is␈αan␈α
instance␈α
of␈α
a␈α
device␈αcalled␈α
a␈α
␈↓αread␈↓ macro.␈α
 In␈α
some␈αsystems␈α
([Moo 76])
␈↓ ↓H␈↓the users may define their own ␈↓αread␈↓ macros.  For example a definition like:

␈↓ ↓H␈↓α␈↓ ¬⊃' <␈↓βr␈↓α= λ[[] list[QUOTE;read[]]]

␈↓ ↓H␈↓might␈α
signal␈αLISP␈α
to␈α
change␈αthe␈α
character␈αtable␈α
for␈α
"'"␈αto␈α
be␈αa␈α
␈↓αread␈↓ macro␈α
and␈αwhen␈α
an␈αinstance␈α
of
␈↓ ↓H␈↓"'"␈α⊃was␈α⊂recognized,␈α⊃the␈α⊂input␈α⊃string␈α⊂would␈α⊃be␈α⊂passed␈α⊃to␈α⊂␈↓αread␈↓,␈α⊃the␈α⊂resulting␈α⊃S-expr␈α⊃␈↓αlist␈↓-ed␈α⊂with
␈↓ ↓H␈↓␈↓αQUOTE␈↓,␈αand␈αreturned␈αto␈α␈↓αread␈↓.␈α MacLISP␈αalso␈αdefines␈αa␈αcomment␈αfacility␈αusing␈αa␈α␈↓αread␈↓ macro.␈α The
␈↓ ↓H␈↓occurrence␈αof␈αa␈αsemi-colon␈αsignals␈αthe␈αbeginning␈αof␈αa␈αcomment;␈αall␈αcharacters␈αto␈αthe␈αend␈αof␈αthe␈αline
␈↓ ↓H␈↓are consider commentary.

␈↓ ↓H␈↓Several␈α∞implementations␈α∞also␈α∂include␈α∞devices␈α∞to␈α∞decrease␈α∂the␈α∞number␈α∞of␈α∞parentheses␈α∂needed.␈α∞For
␈↓ ↓H␈↓example␈α"["␈α
and␈α"]"␈α
are␈αoften␈α
defined␈αto␈α
be␈α"super-parentheses".␈α
 The␈α"["␈α
acts␈αlike␈α
a␈α"("␈α
but␈αits␈α
scope
␈↓ ↓H␈↓runs␈αto␈αthe␈αnext␈α"]",␈αconstructing␈αsufficient␈α")"␈αto␈αbalance␈αthe␈αintervening␈αexpression.␈α Similarly,␈αthe
␈↓ ↓H␈↓scope␈α∂of␈α∂a␈α⊂"]"␈α∂extends␈α∂to␈α⊂the␈α∂prior␈α∂matching␈α∂"[";␈α⊂if␈α∂none␈α∂exists,␈α⊂the␈α∂expression␈α∂is␈α⊂completed␈α∂by
␈↓ ↓H␈↓supplying sufficient ")" to balance.

␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓α␈↓ αV((A B) ((C D E))) = ((A B) ((C D E))] = [(A B) ((C D E))] = ((A B) ((C D E]

␈↓ ↓H␈↓Regardless␈α↔of␈α↔the␈α↔specifics␈α↔of␈α↔the␈α_implementation,␈α↔the␈α↔input␈α↔routines␈α↔will␈α↔read␈α↔in␈α_a␈α↔list
␈↓ ↓H␈↓representation␈α∂of␈α⊂a␈α∂LISP␈α∂expression␈α⊂and␈α∂convert␈α∂it␈α⊂into␈α∂an␈α∂S-expr.␈α⊂For␈α∂example,␈α∂let's␈α⊂see␈α∂what
␈↓ ↓H␈↓happens if we want to evaluate
␈↓ ↓H␈↓α␈↓ ε eq[x;A].

␈↓ ↓H␈↓This will be presented to the machine as:
␈↓ ↓H␈↓α␈↓ ¬N(EQ X (QUOTE A))

␈↓ ↓H␈↓␈↓αread␈↓␈αwill␈αbegin␈αparsing␈αthe␈αsequence␈αof␈αcharacters;␈αit␈αwill␈αdepend␈αon␈α␈↓αratom␈↓␈αto␈αreturn␈αindications␈αof
␈↓ ↓H␈↓the␈αspecial␈αcharacters,␈αand␈αwill␈αdepend␈αon␈α␈↓αratom␈↓␈αto␈αproperly␈αrepresent␈αeach␈αoccurrence␈αof␈αan␈αatom.
␈↓ ↓H␈↓The␈αparser␈αknows␈αabout␈αthe␈αrepresentation␈αwe␈αhave␈αchosen␈αfor␈αlists␈αand␈αwill␈αuse␈α␈↓αcons␈↓␈αto␈αbuild␈αup
␈↓ ↓H␈↓the S-expression form:
␈↓"␈↓ ↓H␈↓
        ⊂ααααπααα⊃  ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
        ~ EQ ~ #αβα→~ X ~ #αβα→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
        %αααα∀ααα$  %ααα∀ααα$  %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                                 ~ ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                                 %→~ QUOTE ~ #αβα→~ A ~≤'~
␈↓"␈↓ ↓H␈↓
                                   %ααααααα∀ααα$  %ααα∀αα$
␈↓ ↓H␈↓␈↓↓216  static structure␈↓ *5.11␈↓


␈↓ ↓H␈↓The␈αreferences␈α
to␈αthe␈αatoms␈α
␈↓αEQ,␈αX,␈α
A,␈↓␈αand␈α␈↓αQUOTE␈↓␈α
are␈αactually␈α
pointers␈αto␈αthe␈α
atoms.␈αEach␈αatom␈α
is
␈↓ ↓H␈↓located only once by the reader. After that we have direct access to atom and its property list.

␈↓ ↓H␈↓Since the input routines perform several ␈↓αcons␈↓ operations; we should look at the details of ␈↓αcons␈↓.



␈↓ ↓H␈↓␈↓ ¬.␈↓↓5.12  A first look at ␈↓αcons␈↓␈↓α


␈↓ ↓H␈↓The␈α
effect␈α
of␈α
the␈α
␈↓αcons␈↓␈α
function␈α
is␈α
quite␈α
different␈α
from␈α
that␈α
of␈α
the␈α
other␈α
LISP␈α
primitives.␈α
 The␈α
other
␈↓ ↓H␈↓functions␈α⊃(or␈α⊃predicates)␈α⊃manipulate␈α⊃existing␈α⊃S-expressions,␈α⊃whereas␈α⊃␈↓αcons␈↓␈α⊃must␈α⊃construct␈α⊃a␈α⊃new
␈↓ ↓H␈↓S-expression␈αfrom␈αtwo␈αexisting␈αS-exprs.␈α That␈αis,␈αgiven␈αrepresentations␈αof␈αtwo␈αS-exprs,␈αsay␈α␈↓αx␈↓␈αand␈α␈↓αy,
␈↓ ↓H␈↓αcons[x;y]␈↓␈α⊃is␈α⊃to␈α⊃get␈α⊃a␈α⊃new␈α⊃cell,␈α⊃put␈α⊃the␈α∩representation␈α⊃of␈α⊃␈↓αx␈↓␈α⊃in␈α⊃the␈α⊃␈↓αcar␈↓-part␈α⊃of␈α⊃the␈α⊃cell␈α∩and␈α⊃the
␈↓ ↓H␈↓representation of ␈↓αy␈↓ in the ␈↓αcdr␈↓-part and return a pointer to the new cell:

␈↓"␈↓ ↓H␈↓
        ␈↓result of ␈↓αcons[x;y]␈↓

␈↓"␈↓ ↓H␈↓
                ~
␈↓"␈↓ ↓H␈↓
                ~          ⊂αααααπααααα⊃
␈↓"␈↓ ↓H␈↓
                %ααααααααα→~  #  ~  #  ~
␈↓"␈↓ ↓H␈↓
                           %ααβαα∀ααβαα$
␈↓"␈↓ ↓H␈↓
                              ~     ~
␈↓"␈↓ ↓H␈↓
                         ⊂αααα$     %αααα⊃
␈↓"␈↓ ↓H␈↓
                         ~               ~
␈↓"␈↓ ↓H␈↓
                         ↓               ↓
␈↓"␈↓ ↓H␈↓
                ␈↓rep. of ␈↓αx                       ␈↓rep. of ␈↓αy␈↓


␈↓ ↓H␈↓When␈αa␈αcomputation␈αis␈αbegun,␈αonly␈αthe␈αatom␈αstructure␈αfor␈αthe␈αinitial␈αLISP␈αsymbol␈αtable␈αuses␈αcells
␈↓ ↓H␈↓in␈αthe␈αpointer area.␈α
The␈αremaining␈αpointer cells␈α
are␈αlinked␈αtogether␈α
and␈αform␈αthe␈α
␈↓↓free␈αspace␈αlist␈↓␈α
or
␈↓ ↓H␈↓FS␈αlist.␈α Whenever␈α␈↓αcons␈↓␈αneeds␈αa␈αcell,␈αthe␈αfirst␈αcell␈αin␈αthe␈αFS␈αlist␈αis␈αused␈αand␈αthe␈αFS␈αlist␈αis␈αset␈αto␈αthe
␈↓ ↓H␈↓␈↓αrest␈↓ of the FS list. For example the following represents the effect of ␈↓αcons[A;B]␈↓:

␈↓"␈↓ ↓H␈↓
   ␈↓αenv␈↓
            ␈↓αdest␈↓

␈↓"␈↓ ↓H␈↓
   ↓              ↓
␈↓"␈↓ ↓H␈↓
⊂ααπαα⊃        ⊂ααααα⊃
␈↓"␈↓ ↓H␈↓
~  ~ #α→...    ~   #αβα⊃
␈↓"␈↓ ↓H␈↓
εααβααλ        εααπααλ ~
␈↓"␈↓ ↓H␈↓
~  ~ #β→⊃        ...   ↓
␈↓"␈↓ ↓H␈↓
εααβααλ ↓      ~  ~  ~←$
␈↓"␈↓ ↓H␈↓
~  ~ #βαααα⊃   εααβααλ                 Pt␈↓βFS␈↓

␈↓"␈↓ ↓H␈↓
%αα∀αα$ ↓  ↓     ...                    ↓
␈↓"␈↓ ↓H␈↓
        ~  ~   %αα∀αα$                  ~
␈↓"␈↓ ↓H␈↓
        ~  %→ααα→αα→⊃                   ~
␈↓"␈↓ ↓H␈↓
        ↓           ↓                   ↓
␈↓"␈↓ ↓H␈↓
    ⊂απααα⊃        ⊂απααα⊃          ⊂ααπααα⊃ ⊂ααπααα⊃      ⊂ααπαα⊃
␈↓"␈↓ ↓H␈↓
    ~∃~ #αβ→...    ~∃~ #αβ→ ...     ~≤'~ #αβ→~≤'~ #αβ→ ...→~≤'~≤'~
␈↓"␈↓ ↓H␈↓
    %α∀ααα$        %α∀ααα$          %αα∀ααα$ %αα∀ααα$      %αα∀αα$
␈↓"␈↓ ↓H␈↓
     atom A         atom B

␈↓"␈↓ ↓H␈↓
␈↓ ε%␈↓↓Before␈↓
␈↓ ↓H␈↓%25.12␈↓ λ&A first look at %3cons%1     217%*



␈↓"␈↓ ↓H␈↓
   ␈↓αenv␈↓
            ␈↓αdest␈↓

␈↓"␈↓ ↓H␈↓
   ↓              ↓
␈↓"␈↓ ↓H␈↓
⊂ααπαα⊃        ⊂ααααα⊃
␈↓"␈↓ ↓H␈↓
~  ~ #α→...    ~   #αβα⊃
␈↓"␈↓ ↓H␈↓
εααβααλ        εααπααλ ~
␈↓"␈↓ ↓H␈↓
~  ~ #β→⊃        ...  ←$
␈↓"␈↓ ↓H␈↓
εααβααλ ↓      ~  ~ #ααα→ααααα→αααα→⊃
␈↓"␈↓ ↓H␈↓
~  ~ #βαααα⊃   εααβααλ              ↓  Pt␈↓βFS␈↓

␈↓"␈↓ ↓H␈↓
%αα∀αα$ ↓  ↓     ...                ~   ↓
␈↓"␈↓ ↓H␈↓
        ~  ~   %αα∀αα$              ~   %ααα→⊃
␈↓"␈↓ ↓H␈↓
        ~  %→ααα→αα→⊃               ~        ~
␈↓"␈↓ ↓H␈↓
        ↓           ↓               ↓        ↓
␈↓"␈↓ ↓H␈↓
    ⊂απααα⊃        ⊂απααα⊃         ⊂αααπααα⊃ ⊂ααπααα⊃      ⊂ααπαα⊃
␈↓"␈↓ ↓H␈↓
    ~∃~ #αβ→...    ~∃~ #αβ→ ...    ~ # ~ # ~ ~≤'~ #αβ→ ...→~≤'~≤'~
␈↓"␈↓ ↓H␈↓
    %α∀ααα$        %α∀ααα$         %αβα∀αβα$ %αα∀ααα$      %αα∀αα$
␈↓"␈↓ ↓H␈↓
      ↑              ↑               ↓   ↓
␈↓"␈↓ ↓H␈↓
      %ααααα←ααααα←ααααα←ααααα←αααααα$   ~
␈↓"␈↓ ↓H␈↓
                     ↑                   ↓
␈↓"␈↓ ↓H␈↓
                     %←ααααα←ααααα←αααααα$

␈↓"␈↓ ↓H␈↓
␈↓ ε,␈↓↓After␈↓

␈↓ ↓H␈↓As␈αthe␈αcomputation␈αcontinues,␈αcells␈αare␈αtaken␈αfrom␈αthe␈αFS␈αlist.␈α When␈αa␈α␈↓αcons␈↓␈αoperation␈αneeds␈αa␈αcell
␈↓ ↓H␈↓and␈α⊂the␈α⊂FS␈α⊂list␈α⊂is␈α⊂empty,␈α⊂the␈α∂computation␈α⊂is␈α⊂suspended␈α⊂and␈α⊂the␈α⊂␈↓↓storage␈α⊂reclaimer␈↓␈α⊂or␈α∂␈↓↓garbage
␈↓ ↓H␈↓↓collector␈↓ is called. The job of the garbage collector is to locate cells for a new FS list.



␈↓ ↓H␈↓␈↓ ∧≥␈↓↓5.13  Storage management: garbage collection␈↓


␈↓ ↓H␈↓During␈α⊂the␈α∂course␈α⊂of␈α⊂a␈α∂computation,␈α⊂contents␈α⊂of␈α∂cells␈α⊂which␈α∂were␈α⊂taken␈α⊂from␈α∂the␈α⊂FS␈α⊂list␈α∂often
␈↓ ↓H␈↓become unnecessary. For example, if we ask LISP to evaluate something as simple as:

␈↓ ↓H␈↓␈↓ βs␈↓α(CONS (QUOTE A)(QUOTE B)),␈↓ many cells are used: 

␈↓ ↓H␈↓␈↓↓1.␈↓ At least seven cells are needed just to read in the expression:

␈↓"␈↓ ↓H␈↓
⊂ααααααπααα⊃  ⊂αααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
~ CONS ~ #αβα→~ # ~ #αβα→~ # ~≤'~
␈↓"␈↓ ↓H␈↓
%αααααα∀ααα$  %αβα∀ααα$  %αβα∀αα$
␈↓"␈↓ ↓H␈↓
                ↓          ↓ ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                ~          %→~ QUOTE ~ #αβα→~ B ~≤'~
␈↓"␈↓ ↓H␈↓
                ~            %ααααααα∀ααα$  %ααα∀αα$
␈↓"␈↓ ↓H␈↓
                ~  ⊂αααααααπααα⊃  ⊂αααπαα⊃
␈↓"␈↓ ↓H␈↓
                %α→~ QUOTE ~ #αβα→~ A ~≤'~
␈↓"␈↓ ↓H␈↓
                   %ααααααα∀ααα$  %ααα∀αα$


␈↓ ↓H␈↓If some of the atoms are not present in the atom table, more cells will be needed.
␈↓ ↓H␈↓␈↓↓218  static structure␈↓ (5.13␈↓


␈↓ ↓H␈↓␈↓↓2.␈↓ One cell will be needed to perform the ␈↓αcons␈↓ operation. See the previous example.

␈↓ ↓H␈↓After␈α
the␈α
computation␈α
is␈α
completed,␈α
LISP␈α
will␈α∞print␈α
"␈↓α(A . B)␈↓"␈α
and␈α
wait␈α
for␈α
more␈α
input.␈α∞After␈α
the
␈↓ ↓H␈↓␈↓αprint␈↓␈α⊂statement␈α⊂is␈α⊂completed␈α⊂none␈α⊂of␈α⊂the␈α∂eight␈α⊂mentioned␈α⊂cells␈α⊂are␈α⊂needed.␈α⊂ They␈α⊂are␈α∂garbage.
␈↓ ↓H␈↓Why␈α
not␈α∞simply␈α
return␈α∞these␈α
`garbage␈α
cells'␈α∞explicitly␈α
to␈α∞the␈α
FS␈α
list?␈α∞ That␈α
could␈α∞be␈α
done␈α∞in␈α
this
␈↓ ↓H␈↓case,␈αbut␈αfrequently␈αit␈αis␈αdifficult␈αto␈αknow␈αexactly␈αwhich␈αcells␈α␈↓¬are␈↓␈αgarbage.␈α Experiments␈αhave␈αbeen
␈↓ ↓H␈↓performed␈αin␈αwhich␈αLISP␈αprogrammers␈αwere␈αallowed␈αto␈αreturn␈α`garbage'␈αto␈αthe␈αFS␈αlist␈αthemselves.
␈↓ ↓H␈↓The␈α
results␈α
were␈α
disastrous;␈αoften␈α
list␈α
structure␈α
thought␈αto␈α
be␈α
garbage␈α
was␈αreturned␈α
to␈α
the␈α
FS␈αlist␈α
by
␈↓ ↓H␈↓the␈α∞programmers,␈α∂who␈α∞were␈α∂unaware␈α∞it␈α∂was␈α∞still␈α∂being␈α∞used␈α∂by␈α∞other␈α∂computations.␈α∞We␈α∂will␈α∞see
␈↓ ↓H␈↓where these difficulties arise later.

␈↓ ↓H␈↓Therefore␈α⊃the␈α⊃responsibility␈α⊃for␈α⊃reclamation␈α⊃is␈α⊃passed␈α⊃to␈α⊃the␈α⊃LISP␈α⊃system.␈α⊃ The␈α⊃␈↓αcons␈↓␈α⊃function
␈↓ ↓H␈↓removes␈α
cells␈α∞from␈α
the␈α∞FS␈α
list,␈α∞and␈α
its␈α
FWS␈α∞counterpart␈α
␈↓αfwcons␈↓,␈α∞removes␈α
cells␈α∞from␈α
the␈α∞FWS␈α
list
␈↓ ↓H␈↓when␈α⊂making␈α⊂numbers␈α⊂or␈α⊂print-names.␈α⊂ These␈α⊃two␈α⊂functions␈α⊂are␈α⊂the␈α⊂only␈α⊂functions␈α⊃allowed␈α⊂to
␈↓ ↓H␈↓manipulate the free storage lists.  When either list becomes empty, the garbage collector is called.

␈↓ ↓H␈↓␈↓↓␈↓ βCThe fundamental assumption behind garbage collection is:␈↓ 

␈↓ ↓H␈↓␈↓ αhAt␈α
any␈α
point␈α
in␈α
a␈α
LISP␈α∞computation,␈α
all␈α
cells␈α
which␈α
contain␈α
parts␈α∞of␈α
the
␈↓ ↓H␈↓␈↓ αhcomputation␈α∪are␈α∪reachable␈α∪(by␈α∀␈↓αcar-cdr␈↓␈α∪chains)␈α∪through␈α∪a␈α∪fixed␈α∀set␈α∪of
␈↓ ↓H␈↓␈↓ αhknown cells or base registers.

␈↓ ↓H␈↓The␈α
first␈α
phase␈α
of␈αthe␈α
garbage␈α
collector,␈α
called␈αthe␈α
␈↓↓marking␈α
phase␈↓,␈α
marks␈αall␈α
of␈α
the␈α
list␈αstructure
␈↓ ↓H␈↓which␈αis␈αcurrently␈αactive␈α(reachable␈αthrough␈αthe␈αabove␈αmentioned␈αfixed␈αcells).␈αThis␈αshould␈αinclude
␈↓ ↓H␈↓all␈α∂the␈α∂atoms␈α∂in␈α∂the␈α∂atom␈α∂table␈α∂and␈α∂all␈α∞the␈α∂cells␈α∂reachable␈α∂by␈α∂␈↓αcar-cdr␈↓␈α∂chains␈α∂from␈α∂any␈α∂of␈α∞these
␈↓ ↓H␈↓atoms.␈αAny␈αpartial␈αcomputations␈αwhich␈αhave␈αbeen␈αgenerated␈αmust␈αalso␈αbe␈αmarked.␈αIn␈αterms␈αof␈αour
␈↓ ↓H␈↓discussion,␈αwe␈αmark␈αfrom␈αthe␈αoblist,␈αfrom␈α␈↓αdest␈↓,␈αfrom␈α␈↓αcontrol␈↓␈α(see␈αpage 152).␈αIf␈αdeep␈αbinding␈αis␈αused,
␈↓ ↓H␈↓we␈α∞mark␈α
the␈α∞elements␈α
reachable␈α∞through␈α
␈↓αenv␈↓.␈α∞ If␈α
shallow␈α∞binding␈α
is␈α∞used,␈α
then␈α∞marking␈α∞of␈α
␈↓αoblist␈↓
␈↓ ↓H␈↓will␈αcapture␈αall␈αthe␈αvalues;␈αeven␈αthe␈αones␈αwhich␈αmay␈αnot␈αbe␈αaccessible␈αas␈αλ-values.␈αWhat␈αwe␈αshould
␈↓ ↓H␈↓do␈α∩instead␈α∩is␈α⊃mark␈α∩the␈α∩non-value␈α∩properties␈α⊃on␈α∩atoms␈α∩using␈α∩␈↓αoblist␈↓;␈α⊃we␈α∩then␈α∩mark␈α∩the␈α⊃values
␈↓ ↓H␈↓separately␈α
using␈α
the␈α
current␈α
␈↓αenv␈↓␈α
skeleton␈α
tree.␈α Once␈α
all␈α
the␈α
active␈α
structure␈α
has␈α
been␈α
marked,␈αwe
␈↓ ↓H␈↓proceed to the ␈↓↓sweep phase.␈↓

␈↓ ↓H␈↓When␈αthe␈αmarking␈αhas␈α
been␈αcompleted,␈αwe␈αgo␈αlinearly␈α
(sweep)␈αthrough␈αmemory,␈αcollecting␈αall␈α
those
␈↓ ↓H␈↓cells␈α∞which␈α
have␈α∞not␈α
been␈α∞marked.␈α∞ Again,␈α
the␈α∞assumption␈α
is␈α∞that␈α∞if␈α
cells␈α∞are␈α
not␈α∞marked␈α∞in␈α
the
␈↓ ↓H␈↓first␈α∩phase,␈α∩then␈α⊃they␈α∩do␈α∩not␈α⊃contain␈α∩information␈α∩necessary␈α⊃to␈α∩the␈α∩LISP␈α∩computation.␈α⊃ These
␈↓ ↓H␈↓unmarked␈αcells␈α
are␈αchained␈α
together␈αvia␈α
their␈α␈↓αcdr␈↓-parts␈αto␈α
form␈αa␈α
new␈αFS␈α
list.␈αThe␈α
FS␈αpointer␈αis␈α
set
␈↓ ↓H␈↓to the beginning of this list.  The unmarked cells in FWS comprise the new FWS list.

␈↓ ↓H␈↓If␈α
there␈α
is␈α
sufficient␈α
room␈α
in␈α
a␈α
full␈α
word␈α
to␈α
contain␈α
a␈α
pointer,␈α
then␈α
we␈α
chain␈α
the␈α∞words␈α
together;
␈↓ ↓H␈↓otherwise we must designate the FWS list some other way.
␈↓ ↓H␈↓␈↓↓5.13␈↓ εXStorage management: garbage collection     219␈↓


␈↓ ↓H␈↓␈↓ ∧¬␈↓↓A simple LISP garbage collector written in LISP␈↓


␈↓ ↓H␈↓We␈α
will␈αnow␈α
write␈αa␈α
garbage␈αcollector␈α
in␈αLISP␈α
to␈αmark␈α
and␈αsweep␈α
nodes␈αin␈α
FS␈αand␈α
FWS.␈α More
␈↓ ↓H␈↓complex␈α∀algorithms␈α∀will␈α∀be␈α∀discussed␈α∀in␈α∪Section ␈α∀and␈α∀on␈α∀page .␈α∀The␈α∀present␈α∀algorithm␈α∪will
␈↓ ↓H␈↓have three main functions:

␈↓ ↓H␈↓␈↓αinitialize[x;y]␈↓ to initialize the marking device for each cell in the space between ␈↓αx␈↓ and ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓αmark[l]␈↓ to do the actual marking of a list ␈↓αl␈↓.

␈↓ ↓H␈↓␈↓αsweep[x;y]␈↓ to collect all inaccessible cells in the space delimited by ␈↓αx␈↓ and ␈↓αy␈↓.

␈↓ ↓H␈↓␈↓αinitialize␈↓␈α∞will␈α
be␈α∞called␈α
twice;␈α∞once␈α
for␈α∞FS␈α
and␈α∞once␈α
for␈α∞FWS.␈α
 ␈↓αmark␈↓␈α∞will␈α
be␈α∞called␈α
for␈α∞each␈α
base
␈↓ ↓H␈↓register␈αwhich␈αpoints␈αto␈αactive␈αlist␈αstructure.␈α The␈αbasic␈αstructure␈αof␈α␈↓αmark␈↓␈αis:␈αif␈αthe␈αword␈αis␈αin␈αFWS
␈↓ ↓H␈↓mark␈αit␈α
and␈αreturn;␈αif␈α
the␈αword␈αhas␈α
already␈αbeen␈αmarked␈α
simply␈αreturn␈αsince␈α
we␈αare␈α
assured␈αthat
␈↓ ↓H␈↓any␈αcells␈αfurther␈α
down␈αthe␈αstructure␈α
have␈αalready␈αbeen␈α
marked.␈αOtherwise␈αthe␈α
word␈αis␈αin␈α
FS␈αand
␈↓ ↓H␈↓thus has a ␈↓αcar␈↓ and a ␈↓αcdr␈↓; mark the word; recursively mark the ␈↓αcar␈↓; recursively mark the ␈↓αcdr␈↓.

␈↓ ↓H␈↓␈↓αsweep␈↓␈α
will␈α
be␈α
called␈α
twice;␈α
once␈α
to␈α
generate␈α
a␈α
new␈α
free␈α
space␈α
list␈α
and␈α
once␈α
to␈α
generate␈α
a␈α∞new␈α
full
␈↓ ↓H␈↓word␈α⊃space␈α⊃list.␈α⊃Elements␈α⊃of␈α⊃these␈α⊃free␈α⊃lists␈α⊃will␈α⊃be␈α⊃chained␈α⊃together␈α⊃by␈α⊃their␈α⊃␈↓αcdr␈↓␈α⊃parts.␈α⊃ The
␈↓ ↓H␈↓initialization␈α⊂and␈α⊂sweep␈α∂phases␈α⊂of␈α⊂this␈α⊂garbage␈α∂collector␈α⊂are␈α⊂very␈α∂similar;␈α⊂both␈α⊂phases␈α⊂must␈α∂be
␈↓ ↓H␈↓assured of reaching every node in the space.

␈↓ ↓H␈↓These main functions use several other functions and predicates:

␈↓ ↓H␈↓␈↓αfwswrdp[x]␈↓␈αis␈αtrue␈αjust␈α
in␈αthe␈αcase␈αthat␈α
␈↓αx␈↓␈αis␈αa␈αword␈α
in␈αFWS.␈αThis␈αis␈α
used␈αas␈αone␈αof␈αthe␈α
termination
␈↓ ↓H␈↓␈↓ αhconditions of ␈↓αmark␈↓.

␈↓ ↓H␈↓␈↓αmarkA[x]␈↓ marks word ␈↓αx␈↓ as accessible.

␈↓ ↓H␈↓␈↓αmarkNA[x]␈↓ marks word ␈↓αx␈↓ as not accessible.

␈↓ ↓H␈↓␈↓αAp[x]␈↓ is true if word ␈↓αx␈↓ is marked "accessible".

␈↓ ↓H␈↓␈↓αup[x]␈↓: If ␈↓αx␈↓ is at location ␈↓
n␈↓ then ␈↓αup[x]␈↓ is location ␈↓
n+1␈↓.

␈↓ ↓H␈↓␈↓αrplacd[x;y]␈↓ modifies ␈↓αx␈↓ by replacing its ␈↓αcdr␈↓-part with ␈↓αy␈↓.  The value returned is the new ␈↓αx␈↓.
␈↓ ↓H␈↓%2220  static structure␈↓ 	5.13%*




␈↓"␈↓ ↓H␈↓
   ␈↓αdest␈↓
                            ␈↓αenv␈↓

␈↓"␈↓ ↓H␈↓
    ~                              ~
␈↓"␈↓ ↓H␈↓
    ~   ⊂ααααααα⊃                  ~    ⊂ααααααα⊃
␈↓"␈↓ ↓H␈↓
    %→  ~     #αβα⊃                %→   ~     #αβαα ### →
␈↓"␈↓ ↓H␈↓
        εαααπαααλ ↓                     εαααπαααλ
␈↓"␈↓ ↓H␈↓
          # # #  ←$                     ~ x ~ # βα→α⊃
␈↓"␈↓ ↓H␈↓
        ~   ~   β→ - - →⊃               εαααβαααλ   ~
␈↓"␈↓ ↓H␈↓
        εαααβαααλ                       ~ y ~ #αβ→⊃ ↓
␈↓"␈↓ ↓H␈↓
        ~   ~   ~       ↓               %ααα∀ααα$ ↓ ~
␈↓"␈↓ ↓H␈↓
          # # #       ⊂αααπααα⊃         ⊂ααααααα⊃ ~ ↓
␈↓"␈↓ ↓H␈↓
        εαααβαααλ     ~ ? ~ # β→ - - - →~   ?   ~←$ ~
␈↓"␈↓ ↓H␈↓
        ~   ~   ~     %ααα∀ααα$         %ααααααα$   ~
␈↓"␈↓ ↓H␈↓
        %ααα∀ααα$       ↑                           ↓
␈↓"␈↓ ↓H␈↓
                        %←ααααα←αααα←ααααα←ααααα←ααα$
␈↓"␈↓ ↓H␈↓↓␈↓ ¬CAlgorithm for ␈↓αrplacd␈↓

␈↓ ↓H␈↓Can you write ␈↓αrplacd␈↓ as a LISP function?

␈↓ ↓H␈↓αinitialize <= λ[[x;y] prog[[]
␈↓ ↓H␈↓α␈↓ β( a␈↓ βHmarkNA[x];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHx ← up[x];
␈↓ ↓H␈↓α␈↓ β(␈↓ βH[eq[x;y] → return[␈↓
t␈↓α]];
␈↓ ↓H␈↓α␈↓ β(␈↓ βHgo [a]]]


␈↓ ↓H␈↓αmark <=    λ[[l] [␈↓ β(Ap[l] → ␈↓
t␈↓α;
␈↓ ↓H␈↓α␈↓ β(fwswrdp[l] → markA[l];
␈↓ ↓H␈↓α␈↓ β(␈↓
t␈↓α → markA[l]; mark[car[l]]; mark[cdr[l]] ]]


␈↓ ↓H␈↓αsweep <= λ[[x;y] prog[[z]
␈↓ ↓H␈↓α␈↓ βλ␈↓ β(z ← ();
␈↓ ↓H␈↓α␈↓ βλ a␈↓ β([not[Ap[x]] →  z ← rplacd[ x;z]];
␈↓ ↓H␈↓α␈↓ βλ␈↓ β(x ← up[x];
␈↓ ↓H␈↓α␈↓ βλ␈↓ β([eq[x;y] → return [z]];
␈↓ ↓H␈↓α␈↓ βλ␈↓ β(go[a]]]


␈↓ ↓H␈↓As␈α
indicated␈αabove,␈α
there␈αare␈α
alternatives␈α
to␈αgarbage␈α
collection.␈α If␈α
the␈αdata-structure␈α
manipulations
␈↓ ↓H␈↓are␈αparticularly␈αsimple␈αthen␈αleaving␈αstorage␈αmanagement␈αin␈αthe␈αprogrammer's␈αhands␈αmight␈αsuffice.
␈↓ ↓H␈↓However, LISP generates very intertwined structures.

␈↓ ↓H␈↓Perhaps␈αthere␈αis␈αanother␈αway␈αto␈αallow␈αthe␈αsystem␈αto␈αhandle␈αstorage␈αmanagement.␈α First␈αnotice␈αthat
␈↓ ↓H␈↓storage␈αmanagement␈αbecomes␈αquite␈αsimple␈αif␈αthere␈αis␈αno␈αsharing␈αof␈αsublists.␈αThe␈αquestion␈αof␈αwhen
␈↓ ↓H␈↓␈↓↓5.13␈↓ εXStorage management: garbage collection     221␈↓


␈↓ ↓H␈↓to␈α
return␈αa␈α
list␈αto␈α
the␈αfree␈α
space␈α
list␈αis␈α
easily␈αsolved.␈α
However␈αit␈α
is␈αdesirable␈α
to␈α
share␈αsubstructure
␈↓ ↓H␈↓quite␈α∞often.␈α∞ Sharing␈α
can␈α∞obviously␈α∞save␈α
space␈α∞and␈α∞careful␈α
modification␈α∞of␈α∞shared␈α∞structures␈α
can
␈↓ ↓H␈↓communicate global information between algorithms.

␈↓ ↓H␈↓Instead␈αof␈αusing␈αa␈αgarbage␈αcollector,␈αwe␈αmight␈αassociate␈αa␈αcounter,␈αcalled␈αa␈α␈↓↓reference␈αcounter␈↓,␈αwith
␈↓ ↓H␈↓each␈αlist␈αwhen␈α
it␈αis␈αbuilt.␈α
In␈αthat␈αcounter␈α
we␈αwill␈αstore␈α
the␈αnumber␈αof␈α
references␈αto␈αthat␈α
list.␈αThus
␈↓ ↓H␈↓the␈αcounter␈αwill␈α
be␈αinitialized␈αto␈α
1␈αwhen␈αthe␈α
list␈αis␈αcreated.␈α
 Whenever␈αthe␈αlist␈α
is␈αshared␈αwe␈α
increase
␈↓ ↓H␈↓the␈αcounter␈αby␈α1;␈αwhenever␈αthe␈αlist␈αis␈αno␈α
longer␈αto␈αbe␈αshared␈αby␈αsome␈αlist␈αstructure,␈αwe␈αdecrease␈α
the
␈↓ ↓H␈↓counter␈α
by␈α
1.␈α
 When␈αthe␈α
count␈α
goes␈α
to␈α0␈α
we␈α
can␈α
put␈α
the␈αcells␈α
of␈α
the␈α
list␈αin␈α
the␈α
free␈α
space␈α
list.␈α A
␈↓ ↓H␈↓difficulty␈α∂with␈α∞the␈α∂reference␈α∂counter␈α∞scheme␈α∂is␈α∂the␈α∞inability␈α∂to␈α∂collect␈α∞circular␈α∂lists.␈α∂Consider␈α∞the
␈↓ ↓H␈↓following sequence:

␈↓ ↓H␈↓␈↓↓1.␈↓ Manufacture a list, ␈↓αx␈↓: ␈↓αx ← (B I G M O T H E R L I S T)␈↓. Reference count is 1.
␈↓ ↓H␈↓␈↓↓2.␈↓ Circularize it: ␈↓αx ← circle[x];␈↓.  Reference count is now 2.
␈↓ ↓H␈↓␈↓↓3.␈↓ Delete all references to ␈↓αx␈↓: ␈↓αx ← NIL␈↓. Reference count reverts to 1.

␈↓ ↓H␈↓The␈α
list␈αis␈α
no␈α
longer␈αreferenced,␈α
but␈αit␈α
is␈α
not␈αon␈α
the␈α
free␈αspace␈α
list,␈αand␈α
has␈α
thus␈αbeen␈α
lost␈α
to␈αthe
␈↓ ↓H␈↓system.

␈↓ ↓H␈↓Two␈αless␈αserious␈α
considerations␈αshould␈αbe␈αmentioned␈α
in␈αconjunction␈αwith␈αreference␈α
counters.␈αFirst,
␈↓ ↓H␈↓each␈α∂node␈α∂which␈α∂is␈α∂to␈α∂be␈α∂collected␈α∂with␈α∂this␈α∂scheme␈α∂must␈α∂have␈α∂an␈α∂associated␈α∂reference␈α⊂field␈α∂to
␈↓ ↓H␈↓contain␈α
the␈α
count.␈α
That␈αrequires␈α
extra␈α
space,␈α
and␈α
usually␈αimposes␈α
a␈α
maximum␈α
size␈α
for␈αthe␈α
reference
␈↓ ↓H␈↓count.␈α If␈αthat␈αmaximum␈αis␈αreached,␈αeither␈αan␈αadditional␈αspace␈αis␈αallocated,␈αor␈αthe␈αfilled␈αcount␈αmay
␈↓ ↓H␈↓never be decremented and the associated structure must be garbage collected.

␈↓ ↓H␈↓The␈α⊃second␈α⊂problem␈α⊃involves␈α⊂decrementing␈α⊃counts.␈α⊂Whenever␈α⊃a␈α⊂count␈α⊃goes␈α⊂to␈α⊃zero␈α⊃the␈α⊂counts
␈↓ ↓H␈↓associated␈α⊃with␈α∩its␈α⊃immediate␈α∩predecessors␈α⊃must␈α⊃also␈α∩be␈α⊃decremented.␈α∩ This␈α⊃process␈α∩is␈α⊃applied
␈↓ ↓H␈↓recursively until non-zero counts are encountered.  The bookeeping for such a task is non-trivial.

␈↓ ↓H␈↓However␈α⊃there␈α∩are␈α⊃significant␈α∩storage␈α⊃management␈α∩problems␈α⊃which␈α∩are␈α⊃amenable␈α∩to␈α⊃reference
␈↓ ↓H␈↓counting.␈α
Parts␈α
of␈α
the␈α
implementation␈α
of␈α∞LISP␈α
systems␈α
can␈α
profitably␈α
use␈α
reference␈α∞counting.␈α
We
␈↓ ↓H␈↓will discuss some of these aspects in Section 5.18.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓I␈α∂This␈α∞problem␈α∂deals␈α∂with␈α∞what␈α∂is␈α∂known␈α∞in␈α∂LISP␈α∞as␈α∂␈↓↓hash␈α∂consing␈↓␈α∞([Got 74]).␈α∂ We␈α∂have␈α∞been
␈↓ ↓H␈↓storing␈α
atoms␈α
uniquely,␈α
but␈α
it␈α
should␈α
be␈α
clear␈α
from␈α
the␈α
behavior␈α
of␈α
␈↓αcons␈↓␈α
that␈α
non-atomic␈αS-exprs
␈↓ ↓H␈↓are␈α⊃␈↓↓not␈↓␈α⊃stored␈α⊃uniquely.␈α⊃ Certainly␈α⊃storing␈α∩single␈α⊃copies␈α⊃of␈α⊃any␈α⊃S-expr␈α⊃would␈α⊃save␈α∩space.␈α⊃For
␈↓ ↓H␈↓example,␈α∞the␈α∞non-atomic␈α∞structure␈α
of␈α∞␈↓α((A␈α∞.␈α∞B).(A␈α
.␈α∞B))␈↓␈α∞could␈α∞be␈α
represented␈α∞with␈α∞two␈α∞cells␈α
rather
␈↓ ↓H␈↓than␈α∩three.␈α∪ Unique␈α∩storage␈α∩is␈α∪not␈α∩without␈α∩its␈α∪difficulties.␈α∩What␈α∩problems␈α∪do␈α∩you␈α∪foresee␈α∩in
␈↓ ↓H␈↓implementing such a scheme?
␈↓ ↓H␈↓␈↓↓222  static structure␈↓ (5.13␈↓


␈↓ ↓H␈↓II␈α⊂We␈α⊂said␈α⊂on␈α⊃page 203␈α⊂that␈α⊂many␈α⊂LISP␈α⊂computations␈α⊃generate␈α⊂list␈α⊂structure␈α⊂rather␈α⊃than␈α⊂true
␈↓ ↓H␈↓LISP-trees. Give an example.

␈↓ ↓H␈↓III␈αCan␈αyou␈αwrite␈αa␈αLISP␈αfunction␈α␈↓αcircle␈α<=␈αλ[[x]␈α...]␈↓␈αwhich␈αwill␈αtake␈αa␈αlist␈α␈↓αx␈↓␈αand␈αmake␈α
it␈αcircular.
␈↓ ↓H␈↓Thus:

␈↓"␈↓ ↓H␈↓
                             ⊂ααααααααααααα←ααααααααααααα⊃
␈↓"␈↓ ↓H␈↓
⊂αααααααααπααα⊃  ⊂αααααπααα⊃ ↓ ⊂ααααπααα⊃  ⊂αααααααπααα⊃ ↑
␈↓"␈↓ ↓H␈↓
~ NOTHING ~ #αβα→~ CAN ~ #αβα∀→~ GO ~ #αβα→~ WRONG ~ #αβ→$
␈↓"␈↓ ↓H␈↓
%ααααααααα∀ααα$  %ααααα∀ααα$   %αααα∀ααα$  %ααααααα∀ααα$

␈↓ ↓H␈↓This␈α∞list␈α∞is␈α∞circular␈α∞on␈α∞its␈α∞"last␈α∂two"␈α∞elements.␈α∞ Printing␈α∞such␈α∞structures␈α∞is␈α∞not␈α∞possible␈α∂using␈α∞the
␈↓ ↓H␈↓␈↓αprint␈↓ function.

␈↓ ↓H␈↓IV␈α→What␈α→LISP␈α→operations␈α→generate␈α→such␈α→intertwined␈α→structures␈α→that␈α→a␈α→reference␈α→counter
␈↓ ↓H␈↓implementation would be infeasible?



␈↓ ↓H␈↓␈↓ βm␈↓↓5.14  A review of the structure of  the LISP machine␈↓
␈↓ ↓H␈↓%25.14␈↓ ¬rA review of the structure of  the LISP machine     223%*



␈↓ ↓H␈↓We␈αhave␈αa␈αgood␈αportion␈αof␈αthe␈αstorage␈α
conventions␈αfor␈αLISP␈αset␈αout.␈α A␈αdifficult␈αarea␈αinvolves␈α
the
␈↓ ↓H␈↓organization␈α⊂of␈α∂the␈α⊂data␈α⊂structures␈α∂to␈α⊂perform␈α∂the␈α⊂correct␈α⊂binding␈α∂and␈α⊂unbinding␈α⊂of␈α∂variables.
␈↓ ↓H␈↓Before we tackle that, we give a diagram showing the basic structure of LISP memory.

␈↓"␈↓ ↓H␈↓
        ⊂αααααααααααααααααααααααααααααααααααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~      ### THE SUBCONSCIOUS ###␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~ ␈↓αeval␈↓
 and friends␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~ ␈↓αread␈↓
 and ␈↓αprint␈↓
␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~ the garbage collector␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~ the base registers for marking;␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~    these include:␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   FS pointer␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   FWS pointer␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   atom table(␈↓αOBLIST␈↓
) pointer␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   registers for partial results␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~     ␈↓αdest, control␈↓
␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   the access chain␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"␈↓ ↓H␈↓
        εααααααααααααααααααααααααααααααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~      ### POINTER SPACE ###␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~  the free space list␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~  those parts of S-exprs containing␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~    ␈↓αcar␈↓
- and ␈↓αcdr␈↓
-parts.␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"␈↓ ↓H␈↓
        εααααααααααααααααααααααααααααααααααααααααλ
␈↓"␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~       ### FULL WORD SPACE ###␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   the full word space list␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   atom print names␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~   numbers␈↓ πX~
␈↓"␈↓ ↓H␈↓
        ~␈↓ πX~
␈↓"␈↓ ↓H␈↓
        %αααααααααααααααααααααααααααααααααααααααα$



␈↓ ↓H␈↓␈↓ ¬~␈↓↓Structure of LISP memory␈↓
␈↓ ↓H␈↓␈↓↓224  static structure␈↓ '5.15␈↓


␈↓ ↓H␈↓␈↓ ∧m␈↓↓5.15  Implementations of binding␈↓


␈↓ ↓H␈↓In␈α∞Section 4.5␈α
and␈α∞Section 4.11we␈α∞discussed␈α
deep␈α∞binding␈α∞and␈α
shallow␈α∞binding␈α∞respectively.␈α
That
␈↓ ↓H␈↓discussion␈α∩took␈α∩place␈α∩at␈α∩a␈α∩reasonably␈α∩abstract␈α∩level.␈α∩ We␈α∩would␈α∩like␈α∩to␈α∩discuss␈α∩these␈α⊃binding
␈↓ ↓H␈↓implementations␈α∃in␈α∃more␈α∃detail.␈α∃ We␈α∃first␈α∃will␈α∃discuss␈α∃some␈α∃of␈α∃the␈α∃possible␈α∃pitfalls␈α⊗in␈α∃the
␈↓ ↓H␈↓implementation␈α∂of␈α∂LISP;␈α∂then␈α∂we␈α∂will␈α∂give␈α∂deep␈α∂and␈α∂shallow␈α∂implementations␈α∂for␈α∂an␈α∞important
␈↓ ↓H␈↓subset␈α
of␈α
LISP.␈α
 Finally,␈α
we␈α
will␈α
discuss␈α
some␈α
of␈α
the␈α
methods␈α
available␈α
for␈α
implementing␈α
the␈α
full
␈↓ ↓H␈↓language in an efficient manner.

␈↓ ↓H␈↓Though␈α⊂much␈α⊂of␈α⊃this␈α⊂discussion␈α⊂deals␈α⊃with␈α⊂the␈α⊂binding␈α⊂stategies␈α⊃of␈α⊂LISP,␈α⊂and␈α⊃therefore␈α⊂with
␈↓ ↓H␈↓control␈αstructure,␈αwe␈α
are␈αrestricting␈αourselves␈α
to␈αthe␈αdata␈α
structure␈αrequirements.␈αThe␈α
next␈αchapter
␈↓ ↓H␈↓discusses how these data structures are to be manipulated.

␈↓ ↓H␈↓Consider the evaluation of a form:
␈↓ ↓H␈↓α␈↓ ¬←f[a␈↓β1␈↓α; ...;a␈↓βn␈↓α]␈↓ where:
␈↓ ↓H␈↓␈↓α␈↓ ¬¬f <= λ[[x␈↓β1␈↓α; ... x␈↓βn␈↓α] ...g[ ...] ...i[ ...]],
␈↓ ↓H␈↓α␈↓ ¬+g <= λ[[ ...] ...h[ ...] ], ␈↓and ␈↓α
␈↓ ↓H␈↓α␈↓ ¬Xi <= λ[[ ...] ...j[ ...] ]

␈↓ ↓H␈↓Typically␈αa␈αpicture␈α
like␈αthe␈αfollowing␈α
occurs,␈αwhere␈αthe␈α
instance␈αof␈αfunction␈α
name␈αmeans␈αa␈αblock␈α
of
␈↓ ↓H␈↓λ-bindings necessary to begin evaluation of that function:
␈↓"␈↓ ↓H␈↓
                      ⊂ααα⊃                       ⊂ααα⊃
␈↓"␈↓ ↓H␈↓
                      ~ h ~                       ~ j ~
␈↓"␈↓ ↓H␈↓
               ⊂ααα⊃  εαααλ  ⊂ααα⊃         ⊂ααα⊃  εαααλ   ...
␈↓"␈↓ ↓H␈↓
               ~ g ~  ~ g ~  ~ g ~         ~ i ~  ~ i ~
␈↓"␈↓ ↓H␈↓
        ⊂ααα⊃  εαααλ  εαααλ  εαααλ  ⊂ααα⊃  εαααλ  εαααλ   ...
␈↓"␈↓ ↓H␈↓
        ~ f ~  ~ f ~  ~ f ~  ~ f ~  ~ f ~  ~ f ~  ~ f ~
␈↓"␈↓ ↓H␈↓
        %ααα$  %ααα$  %ααα$  %ααα$  %ααα$  %ααα$  %ααα$

␈↓ ↓H␈↓We␈α∞build␈α∞up␈α∞a␈α∞stack␈α∞of␈α
λ-binding␈α∞blocks␈α∞as␈α∞we␈α∞continue␈α∞to␈α
enter␈α∞procedures,␈α∞and␈α∞as␈α∞we␈α∞leave␈α
a
␈↓ ↓H␈↓procedure␈α
we␈αremove␈α
that␈α
block␈αof␈α
bindings␈α
from␈αthe␈α
stack.␈α
 When␈αwe␈α
wish␈α
to␈αknow␈α
the␈α
value␈αof␈α
a
␈↓ ↓H␈↓variable␈αwe␈αlook␈αdown␈αthe␈αstack␈αfor␈αthe␈αfirst␈αoccurrence␈αof␈αthat␈αvariable;␈αthe␈αassociated␈αbinding␈αis
␈↓ ↓H␈↓the desired value.

␈↓ ↓H␈↓LISP␈αallows␈α
functional␈αarguments␈α
and␈αfunctional␈α
values.␈αThese␈α
constructs␈αrequire␈α
modification␈αof
␈↓ ↓H␈↓the behavior modeled in the simple blocks world.

␈↓ ↓H␈↓When␈αwe␈αrecognize␈αa␈αfunctional␈αargument,␈αwe␈αremember␈αthe␈αblock␈αwhich␈αwas␈αcurrently␈αon␈αthe␈αtop
␈↓ ↓H␈↓of␈αthe␈α
stack.␈αWhen␈αwe␈α
apply␈αthat␈αfunctional␈α
argument,␈αintervening␈αblocks␈α
will␈αhave␈α
been␈αstacked;
␈↓ ↓H␈↓we␈α∞change␈α∞the␈α∞environment␈α∞such␈α∞that␈α∞variable␈α∞lookup␈α∞takes␈α∞place␈α∞beginning␈α∞at␈α∞the␈α∂saved␈α∞block,
␈↓ ↓H␈↓rather than at the top of the stack.

␈↓ ↓H␈↓However,␈αif␈α␈↓αh␈↓␈αsay,␈αgenerated␈αa␈αfunctional␈αvalue␈αwhich␈αis␈αto␈αbe␈αapplied␈αin␈αthe␈αcontext␈αof␈α␈↓αj␈↓␈αthen␈αwe
␈↓ ↓H␈↓must␈α∂retain␈α∂those␈α∞values␈α∂in␈α∂the␈α∞␈↓αf-g-h␈↓␈α∂stack␈α∂in␈α∞such␈α∂a␈α∂way␈α∞that␈α∂they␈α∂may␈α∞be␈α∂used␈α∂to␈α∂restore␈α∞the
␈↓ ↓H␈↓enviroment when we desire to apply the functional value in the ␈↓αf-i-j␈↓ stack.
␈↓ ↓H␈↓%25.15␈↓ πvImplementations of binding     225%*


␈↓"␈↓ ↓H␈↓
                      ⊂ααα⊃  ⊂ααα⊃
␈↓"␈↓ ↓H␈↓
                ↑     ~ h ~  ~ j ~   ~
␈↓"␈↓ ↓H␈↓
                ~     εαααλ  εαααλ   ↓
␈↓"␈↓ ↓H␈↓
           bind ~     ~ g ~  ~ i ~   ~ unbind
␈↓"␈↓ ↓H␈↓
                ↑     εααα∀αα∀αααλ   ↓
␈↓"␈↓ ↓H␈↓
                ~     ~     f    ~   ↓
␈↓"␈↓ ↓H␈↓
                      %αααααααααα$

␈↓ ↓H␈↓We␈αwill␈αdiscuss␈αdata␈αstructure␈αrequirements␈αfor␈αimplementation␈αof␈αthese␈αthree␈αLISP␈αsubsets:␈αsimple
␈↓ ↓H␈↓function application, functional arguments, and functional values.

␈↓ ↓H␈↓The␈αmechanism␈α
which␈αwe␈αdescribed␈α
in␈αthe␈αinital␈α
blocks␈αmodel␈αoccurs␈α
quite␈αnaturally␈α
in␈αcomputer
␈↓ ↓H␈↓science.␈αIt␈αis␈αcalled␈αa␈α␈↓↓stack␈↓␈↓π 110␈↓.␈α The␈αimportant␈αcharacteristics␈αof␈αstacks␈αare␈αthat␈αthey␈αare␈αlists␈αsuch
␈↓ ↓H␈↓that additions and deletions can only be made at the front of the list.

␈↓ ↓H␈↓What␈αis␈αof␈α
interest␈αto␈αus␈α
now␈αis␈αthat␈αstacks␈α
have␈αa␈αparticularly␈α
efficient␈αimplementation.␈αDue␈αto␈α
the
␈↓ ↓H␈↓very␈α⊂regular␈α⊂way␈α⊂in␈α⊂which␈α⊂stacks␈α⊃are␈α⊂manipulated,␈α⊂the␈α⊂linked␈α⊂allocation␈α⊂implementation␈α⊃is␈α⊂not
␈↓ ↓H␈↓necessary.  Instead a stack can be implemented as:

␈↓ ↓H␈↓␈↓ αh␈↓↓1.␈↓ A sequence of contiguous locations

␈↓ ↓H␈↓␈↓ αh␈↓↓2.␈↓ A pointer initialized to point ␈↓↓before␈↓ the first of these locations.

␈↓ ↓H␈↓␈↓ αh␈↓↓3.␈↓␈αAn␈αoperation,␈αtypically␈αcalled␈α␈↓↓push␈↓␈αwhich␈αplaces␈αa␈αnew␈αobject␈αin␈αthe␈αstack.␈αThis␈αcan
␈↓ ↓H␈↓␈↓ αhbe␈α∂accomplished␈α∂by␈α∂adding␈α∂1␈α∂to␈α∂the␈α∂value␈α∂of␈α∂the␈α∂stack␈α∂pointer,␈α∂and␈α∂then␈α∂putting␈α∞a
␈↓ ↓H␈↓␈↓ αhrepresentation of the object in the cell currently referenced by the pointer.

␈↓ ↓H␈↓␈↓ αh␈↓↓4.␈↓␈αAn␈αoperation␈α
called␈α␈↓↓pop␈↓␈αwhich␈αgets␈α
the␈αfirst␈αvalue␈αin␈α
the␈αstack␈αand␈αthen␈α
decrements
␈↓ ↓H␈↓␈↓ αhthe pointer by 1.

␈↓ ↓H␈↓␈↓ αh␈↓↓5.␈↓␈αThough␈αthe␈α
abstract␈αstructure␈αof␈α
a␈αstack␈αdoes␈αnot␈α
involve␈αlimitations␈αon␈α
the␈αlength
␈↓ ↓H␈↓␈↓ αhof␈αstack-space,␈αany␈αrepresentation␈αshould␈αinclude␈αtechniques␈αfor␈αassuring␈αthat␈αthe␈αstack
␈↓ ↓H␈↓␈↓ αhpointer stays within its allotted space.

␈↓ ↓H␈↓Notice␈αthat␈αthe␈α␈↓αconcat␈↓␈αoperation␈αcan␈αbe␈αinterpreted␈αas␈α␈↓↓pushing␈↓␈αand␈αthe␈α␈↓αrest␈↓␈αoperation␈αas␈α␈↓↓popping␈↓.
␈↓ ↓H␈↓Indeed␈α∞our␈α∞earlier␈α∞manipulations␈α∞of␈α∞symbol␈α∞tables␈α∞effectively␈α∞used␈α∞such␈α∞stack␈α∞operations.␈α∞This␈α∞is
␈↓ ↓H␈↓particularly apparent in the representation of symbol tables given on page 107.




␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 110␈↓␈αStacks␈αare␈α
closely␈αrelated␈αto␈α
a␈αtheoretical␈αdevice␈αcalled␈α
push-down␈αautomaton.␈αThere,␈α
only␈αthe
␈↓ ↓H␈↓top␈α∩element␈α⊃of␈α∩the␈α∩stack␈α⊃is␈α∩accessible.␈α⊃We␈α∩take␈α∩a␈α⊃more␈α∩pragmatic␈α⊃position,␈α∩allowing␈α∩access␈α⊃to
␈↓ ↓H␈↓elements␈α∞within␈α∞the␈α∂stack,␈α∞and␈α∞indeed␈α∂modification␈α∞of␈α∞elements␈α∞within␈α∂the␈α∞stack;␈α∞but␈α∂removal␈α∞of
␈↓ ↓H␈↓elements␈α∞from␈α∞within␈α∂the␈α∞stack␈α∞is␈α∞not␈α∂allowed.␈α∞To␈α∞remove␈α∞an␈α∂element,␈α∞we␈α∞must␈α∞first␈α∂remove␈α∞the
␈↓ ↓H␈↓elements above it.
␈↓ ↓H␈↓␈↓↓226  static structure␈↓ '5.15␈↓



␈↓ ↓H␈↓We will discuss the binding process in terms of a sequence of three events:

␈↓ ↓H␈↓␈↓↓1.␈↓.␈α␈↓αbind␈↓␈αdescribes␈αwhat␈αthe␈αimplementation␈αdoes␈αwhen␈αwe␈αare␈αready␈αto␈αcall␈αa␈αprocedure.␈αThe␈αactual
␈↓ ↓H␈↓␈↓ α_parameters␈α
are␈αevaluated␈α
and␈αwe␈α
are␈αready␈α
to␈αadd␈α
them␈αto␈α
the␈αenvironment␈α
and␈αevaluate␈α
the
␈↓ ↓H␈↓␈↓ α_body of the procedure.

␈↓ ↓H␈↓␈↓↓2.␈↓ ␈↓αlookup␈↓ will discuss how values are located in the current environment.

␈↓ ↓H␈↓␈↓↓3.␈↓ ␈↓αunbind␈↓ will describe what has to be done as we prepare to exit from a procedure.



␈↓ ↓H␈↓␈↓ ∧*␈↓↓5.16  stack implementation of deep binding␈↓


␈↓ ↓H␈↓The␈αstack␈αimplementation␈αof␈αsimple␈αfunction␈αapplication␈αis␈αa␈αstraightforward␈αstack␈αimplementation
␈↓ ↓H␈↓of␈α∞our␈α∞blocks␈α
picture.␈α∞ We␈α∞have␈α
two␈α∞stacks␈α∞which␈α
operate␈α∞synchronously.␈α∞One␈α
stack␈α∞is␈α∞called␈α
the
␈↓ ↓H␈↓␈↓↓name␈α
stack␈↓;␈α∞the␈α
other␈α∞is␈α
called␈α∞the␈α
␈↓↓value␈α∞stack␈↓.␈α
 The␈α∞name␈α
stack␈α∞maintains␈α
the␈α∞λ-variables␈α
(and
␈↓ ↓H␈↓generated␈α∂names,␈α∂if␈α∂a␈α∂primitive␈α∂is␈α∂called).␈α∂The␈α⊂top␈α∂of␈α∂the␈α∂name␈α∂stack␈α∂defines␈α∂the␈α∂origin␈α⊂of␈α∂the
␈↓ ↓H␈↓environment.␈α∂When␈α∂the␈α∂value␈α∞of␈α∂a␈α∂variable␈α∂is␈α∂requested␈α∞␈↓αlookup␈↓␈α∂proceeds␈α∂down␈α∂the␈α∂name␈α∞stack,
␈↓ ↓H␈↓looking␈α⊂for␈α⊂the␈α⊂first␈α∂occurrence␈α⊂of␈α⊂the␈α⊂variable.␈α⊂The␈α∂corresponding␈α⊂position␈α⊂in␈α⊂the␈α⊂value␈α∂stack
␈↓ ↓H␈↓contains the desired value.

␈↓ ↓H␈↓When␈α∞we␈α
recognize␈α∞a␈α
function␈α∞application,␈α
we␈α∞begin␈α
the␈α∞evaluation␈α
of␈α∞the␈α
actual␈α∞parameters.␈α
As
␈↓ ↓H␈↓each␈α∞parameter␈α∂is␈α∞evaluated,␈α∞the␈α∂result␈α∞is␈α∞pushed␈α∂into␈α∞the␈α∞value␈α∂stack.␈α∞When␈α∞all␈α∂the␈α∞parameters
␈↓ ↓H␈↓have␈α⊂been␈α⊂evaluated,␈α⊂we␈α∂are␈α⊂ready␈α⊂to␈α⊂evaluate␈α∂the␈α⊂body␈α⊂of␈α⊂the␈α∂expression.␈α⊂At␈α⊂that␈α⊂point␈α∂␈↓αbind␈↓
␈↓ ↓H␈↓pushes␈αthe␈αλ-variables␈αonto␈αthe␈αname␈αstack.␈α When␈αwe␈αcomplete␈αthe␈αevaluation␈αof␈αthe␈αbody␈αof␈αthe
␈↓ ↓H␈↓expression␈α∂␈↓αunbind␈↓␈α∂pops␈α∂the␈α∞λ-variables␈α∂off␈α∂of␈α∂the␈α∞name␈α∂stack,␈α∂and␈α∂pops␈α∞their␈α∂values␈α∂off␈α∂of␈α∞the
␈↓ ↓H␈↓value stack.

␈↓ ↓H␈↓Since␈αthe␈αλ-variables␈α
leave␈αthe␈αstack␈α
as␈αa␈αgroup␈αwe␈α
can␈αsometimes␈αspeed␈α
things␈αup␈αby␈αstoring␈α
block
␈↓ ↓H␈↓sizes␈α
in␈αthe␈α
stack.␈αAlso␈α
the␈α
word␈αsize␈α
of␈αthe␈α
machine␈αmay␈α
allow␈α
using␈αone␈α
stack␈αfor␈α
both␈αnames␈α
and
␈↓ ↓H␈↓values. For example:

␈↓"␈↓ ↓H␈↓
        ⊂ααααααααααααα⊃
␈↓"␈↓ ↓H␈↓
        ~      #ααα→ααβ→⊃
␈↓"␈↓ ↓H␈↓
        εαααααααπαααααλ ↓
␈↓"␈↓ ↓H␈↓
        ~ namen ~ valn~ ~
␈↓"␈↓ ↓H␈↓
        εαααααααβαααααλ ~
␈↓"␈↓ ↓H␈↓
             # # #      ↓
␈↓"␈↓ ↓H␈↓
        ~ name2 ~ val2~ ~
␈↓"␈↓ ↓H␈↓
        εαααααααβαααααλ ~
␈↓"␈↓ ↓H␈↓
        ~ name1 ~ val1~ ↓
␈↓"␈↓ ↓H␈↓
        εααααααα∀αααααλ ~
␈↓"␈↓ ↓H␈↓
        ~             ~←$
␈↓"␈↓ ↓H␈↓
        ~      #ααα→ααβ→⊃
␈↓"␈↓ ↓H␈↓
                        ↓
␈↓ ↓H␈↓␈↓↓5.16␈↓ εrstack implementation of deep binding     227␈↓


␈↓ ↓H␈↓Where␈α∞␈↓
val1␈↓␈α∞is␈α∞furthest␈α
down␈α∞the␈α∞stack␈α∞since␈α
it␈α∞was␈α∞pushed␈α∞on␈α
first,␈α∞and␈α∞where␈α∞all␈α∞references␈α
to
␈↓ ↓H␈↓␈↓
namei␈↓␈α
and␈α
␈↓
vali␈↓␈α∞are␈α
really␈α
pointers␈α∞to␈α
to␈α
appropriate␈α
S-expressions␈α∞(atoms␈α
or␈α
dotted␈α∞pairs).␈α
 The
␈↓ ↓H␈↓"back␈αpointer"␈α
is␈αused␈α
for␈αremoving␈αblocks␈α
of␈αbindings,␈α
but␈αit␈αis␈α
also␈αa␈α
representation␈αof␈αthe␈α
control
␈↓ ↓H␈↓link discussed in Section 4.8.

␈↓ ↓H␈↓A␈α∞slight␈α∞modification␈α∞of␈α∂this␈α∞scheme␈α∞is␈α∞sufficient␈α∂to␈α∞support␈α∞evaluation␈α∞of␈α∂functional␈α∞arguments.
␈↓ ↓H␈↓The␈α∂additional␈α∂piece␈α∞of␈α∂information␈α∂guides␈α∞␈↓αlookup␈↓␈α∂in␈α∂its␈α∞search␈α∂for␈α∂the␈α∞next␈α∂block␈α∂of␈α∞bindings.
␈↓ ↓H␈↓Instead␈αof␈αproceeding␈αlinearly␈αdown␈αthe␈αstack,␈α␈↓αlookup␈↓␈αproceeds␈αlinearly␈αthrough␈αa␈αblock␈αand␈αat␈αthe
␈↓ ↓H␈↓end␈α
of␈α
each␈α
block␈α∞is␈α
information␈α
telling␈α
␈↓αlookup␈↓␈α∞where␈α
the␈α
next␈α
block␈α∞of␈α
bindings␈α
is␈α
to␈α∞be␈α
found.
␈↓ ↓H␈↓Recall that information is called the access link (Section 4.8).

␈↓"␈↓ ↓H␈↓
                        ⊂ααααααααααααα⊃
␈↓"␈↓ ↓H␈↓
                      ⊂←β←αα#     #αα→β→⊃
␈↓"␈↓ ↓H␈↓
                      ↓ εαααααααπαααααλ ↓
␈↓"␈↓ ↓H␈↓
                      ~ ~ namen ~ valn~ ~
␈↓"␈↓ ↓H␈↓
                      ~ εαααααααβαααααλ ~
␈↓"␈↓ ↓H␈↓
       access links   ↓      # # #      ↓   control links
␈↓"␈↓ ↓H␈↓
                      ~ ~ name2 ~ val2~ ~
␈↓"␈↓ ↓H␈↓
                      ~ εαααααααβαααααλ ~
␈↓"␈↓ ↓H␈↓
                      ↓ ~ name1 ~ val1~ ↓
␈↓"␈↓ ↓H␈↓
                      ~ εααααααα∀αααααλ ~
␈↓"␈↓ ↓H␈↓
                      %→~             ~←$
␈↓"␈↓ ↓H␈↓
                      ⊂←β←αα#     #αα→β→⊃
␈↓"␈↓ ↓H␈↓
                      ↓                 ↓

␈↓ ↓H␈↓In␈α⊂the␈α⊂majority␈α⊂of␈α⊂cases,␈α⊂the␈α∂access␈α⊂link␈α⊂points␈α⊂at␈α⊂the␈α⊂next␈α∂block␈α⊂down␈α⊂the␈α⊂stack.␈α⊂ The␈α⊂use␈α∂of
␈↓ ↓H␈↓functional␈α↔arguments␈α↔will␈α↔generate␈α↔an␈α↔access␈α↔link,␈α↔pointing␈α↔further␈α↔down␈α↔the␈α↔stack.␈α⊗ The
␈↓ ↓H␈↓␈↓αfunction␈↓-construct␈αis␈αresponsible␈αfor␈αsaving␈αthe␈αbinding␈αenvironment.␈αIn␈αthis␈αimplementation␈αthis␈αis
␈↓ ↓H␈↓accomplished␈α∩by␈α∩saving␈α∩a␈α∩pointer␈α∩to␈α∩the␈α⊃current␈α∩top␈α∩of␈α∩the␈α∩name␈α∩stack.␈α∩ When␈α∩a␈α⊃functional
␈↓ ↓H␈↓argument␈α
is␈α
applied,␈α
the␈α
access␈α
link␈α
will␈α
be␈α
set␈α
to␈α
the␈α
binding␈α
environment␈α
(page 128).␈α
 Since␈α
we␈α
are
␈↓ ↓H␈↓dealing␈αwith␈αfunctional␈αarguments,␈αwe␈αare␈αassured␈αthat␈αthe␈αapplication␈αof␈αthat␈αargument␈αwill␈αoccur
␈↓ ↓H␈↓within␈α
an␈α
expression␈α∞which␈α
dynamically␈α
surrounds␈α
the␈α∞creation␈α
of␈α
the␈α
functional␈α∞argument.␈α
This
␈↓ ↓H␈↓means␈α
that␈αour␈α
environment␈αpointer␈α
will␈αindeed␈α
be␈α
a␈αpointer␈α
down␈αthe␈α
stack.␈αThe␈α
use␈αof␈α
functional
␈↓ ↓H␈↓values␈α∀invalidates␈α∀that␈α∀assumption.␈α∀ Before␈α∀examining␈α∀that␈α∀problem␈α∀we␈α∀will␈α∀discuss␈α∀shallow
␈↓ ↓H␈↓binding for the same LISP subsets.



␈↓ ↓H␈↓␈↓ ∧↔␈↓↓5.17  stack implementation of shallow binding␈↓


␈↓ ↓H␈↓A␈αstack␈αimplementation␈αof␈αshallow␈αbinding␈αallows␈αsome␈αelegant␈αeconomies.␈α Instead␈αof␈αhaving␈αa␈α
set
␈↓ ↓H␈↓of␈α␈↓αVAR␈↓␈αentries␈αassociated␈αwith␈αeach␈αatom,␈αwe␈αwill␈αhave␈α␈↓↓one␈↓␈αvalue cell,␈αand␈αwill␈αmaintain␈αprevious
␈↓ ↓H␈↓bindings␈α∂of␈α∂the␈α∂variable␈α∂on␈α∂a␈α∂stack.␈α∂The␈α∞␈↓αVAR␈↓␈α∂property␈α∂will␈α∂be␈α∂combined␈α∂with␈α∂the␈α∂other␈α∞value
␈↓ ↓H␈↓properties␈αwhich␈αan␈αatom␈αcan␈αcarry.␈αThus␈α␈↓↓any␈αand␈αall␈↓␈αvalue␈αproperties␈αwill␈αbe␈αfound␈αthrough␈αthe
␈↓ ↓H␈↓value cell.

␈↓ ↓H␈↓The␈αimplementation␈αof␈α␈↓αlookup␈↓␈αwill␈αbe␈αsimple:␈αtake␈αthe␈αvalue␈αin␈αthe␈αvalue␈αcell.␈αThe␈αimplementation
␈↓ ↓H␈↓␈↓↓228  static structure␈↓ (5.17␈↓


␈↓ ↓H␈↓of␈α␈↓αmkenv␈↓␈αwill␈αrequire␈αa␈αbit␈αmore␈αwork␈αthan␈αthat␈αexpended␈αin␈αthe␈αdeep␈αbinding␈αcase;␈αand␈αthe␈αwork
␈↓ ↓H␈↓required on leaving a procedure will be more substantial than popping the name-value stack.

␈↓ ↓H␈↓Since␈αthe␈αthe␈αvalue␈αcell␈αwill␈αbe␈αmaintained␈αto␈α␈↓↓always␈↓␈αcontain␈αthe␈αproper␈αbinding␈αof␈αa␈αvariable,␈αthe
␈↓ ↓H␈↓distinction␈α
between␈α
local␈α∞and␈α
non-local␈α
variables␈α
goes␈α∞away.␈α
The␈α
contents␈α
of␈α∞the␈α
value␈α
cell␈α∞is␈α
␈↓↓the␈↓
␈↓ ↓H␈↓current␈α
value␈α
for␈α
any␈αvariable.␈α
 When␈α
we␈α
enter␈αa␈α
procedure,␈α
we␈α
will␈αsave␈α
the␈α
old␈α
contents␈α
of␈αthe
␈↓ ↓H␈↓value␈αcell␈αand␈α
replace␈αit␈αwith␈αthe␈α
new␈αvalue.␈αWhen␈α
we␈αleave␈αa␈αprocedure␈α
we␈αwill␈αrestore␈αthat␈α
saved
␈↓ ↓H␈↓value.␈α∂ Since␈α∂procedures␈α∞can␈α∂be␈α∂called␈α∂recursively␈α∞we␈α∂will␈α∂need␈α∞stack-like␈α∂devices␈α∂for␈α∂saving␈α∞old
␈↓ ↓H␈↓values.

␈↓ ↓H␈↓The␈α∩justification␈α∩for␈α∪the␈α∩implementation␈α∩comes␈α∩from␈α∪an␈α∩examination␈α∩of␈α∩the␈α∪shallow␈α∩binding
␈↓ ↓H␈↓strategy␈α∞as␈α∞described␈α∞in␈α∞Section 4.11.␈α∞ There␈α∂we␈α∞had␈α∞a␈α∞collection␈α∞of␈α∞bindings␈α∞associated␈α∂with␈α∞the
␈↓ ↓H␈↓identifier.␈α Without␈αloss␈αof␈α
generality,␈αwe␈αcan␈αorganize␈α␈↓αmkenv␈↓␈α
such␈αthat␈αthe␈αnew␈αbinding␈α
is␈αadded
␈↓ ↓H␈↓in␈α∂front␈α∂of␈α⊂any␈α∂previous␈α∂binding.␈α⊂ Now␈α∂if␈α∂we␈α∂are␈α⊂only␈α∂evaluating␈α∂simple␈α⊂function␈α∂applications,
␈↓ ↓H␈↓␈↓αlookup␈↓␈α⊂will␈α∂find␈α⊂the␈α∂desired␈α⊂binding␈α∂provided␈α⊂␈↓αunbind␈↓␈α∂removes␈α⊂the␈α∂first␈α⊂binding␈α∂as␈α⊂it␈α⊂exits␈α∂the
␈↓ ↓H␈↓procedure.␈α
 Thus␈α
␈↓αbind␈↓␈↓π 111␈↓␈α
and␈α
␈↓αunbind␈↓␈α
act␈α
on␈αthe␈α
value␈α
entries␈α
in␈α
a␈α
stack-like␈α
fashion.␈α
 Instead␈αof
␈↓ ↓H␈↓associating␈α
a␈α
separate␈α
stack␈α
with␈α
each␈α
variable␈α
and␈α
accessing␈α
the␈α
value␈α
through␈α
the␈α
top␈α
of␈α
the␈α
stack,
␈↓ ↓H␈↓we␈αassociate␈αa␈αsingle␈αvalue␈αcell␈αwith␈αeach␈αvariable␈αand␈αstore␈αthe␈αsaved␈αvalues␈αof␈αall␈αvariables␈αon␈αa
␈↓ ↓H␈↓common stack.

␈↓ ↓H␈↓Since␈α∞we␈α∞allow␈α
atoms␈α∞to␈α∞have␈α
at␈α∞most␈α∞one␈α
value␈α∞property␈α∞at␈α
any␈α∞time,␈α∞we␈α
will␈α∞combine␈α∞all␈α
these
␈↓ ↓H␈↓values␈α
under␈α
the␈α
common␈α
indicator␈α␈↓αVALUE␈↓.␈α
 So␈α
the␈α
value␈α
cell␈αis␈α
the␈α
property␈α
value␈α
of␈αthe␈α
property
␈↓ ↓H␈↓␈↓αVALUE␈↓.␈α Also,␈αwe␈αhave␈αa␈αstack,␈αcalled␈αSP␈αin␈αwhich␈αwe␈αcan␈αsave␈αold␈αvalues␈αof␈αvariables.␈α ␈↓αbind␈↓␈αfirst
␈↓ ↓H␈↓saves␈α∞both␈α∞the␈α∞value␈α∞and␈α∞the␈α∞location␈α∞of␈α
the␈α∞value cell␈α∞for␈α∞each␈α∞variable␈α∞being␈α∞rebound;␈α∞it␈α
then
␈↓ ↓H␈↓puts␈α
a␈α∞special␈α
mark␈α
in␈α∞the␈α
top␈α∞of␈α
the␈α
SP␈α∞stack␈α
to␈α∞delimit␈α
each␈α
block␈α∞of␈α
λ-rebindings.␈α∞All␈α
␈↓αunbind␈↓
␈↓ ↓H␈↓need␈α
do␈α
is␈α
restore␈αthe␈α
first␈α
block␈α
of␈αsaved␈α
values.␈α
 It␈α
knows␈α
the␈αvalues␈α
and␈α
also␈α
knows␈αthe␈α
addresses
␈↓ ↓H␈↓of␈α
the␈α
value cells␈↓π 112␈↓.␈α∞ All␈α
of␈α
the␈α∞information␈α
it␈α
needs␈α∞for␈α
restoration␈α
is␈α∞saved␈α
in␈α
the␈α∞stack.␈α
 This
␈↓ ↓H␈↓stack␈α∞of␈α∞previous␈α∞values␈α
is␈α∞also␈α∞visited␈α∞by␈α∞the␈α
garbage␈α∞collector;␈α∞it␈α∞may␈α
be␈α∞that␈α∞the␈α∞only␈α∞copy␈α
of
␈↓ ↓H␈↓some␈α∞value␈α∞is␈α∞accessible␈α∞only␈α∞through␈α∞the␈α
SP-stack.␈α∞It␈α∞would␈α∞be␈α∞most␈α∞unfortunate␈α∞if␈α∞the␈α
garbage
␈↓ ↓H␈↓collector neglected to mark that entry and the unbinding mechanism later tried to restore the value.










␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 111␈↓ Also known as ␈↓αsend␈↓ on page 133.

␈↓ ↓H␈↓␈↓π 112␈↓␈αAn␈αalternative␈αimplementation␈αof␈α␈↓αunbind␈↓␈αwould␈αonly␈αstore␈αthe␈αsaved␈αvalues,␈αwithout␈αexplicitly
␈↓ ↓H␈↓saving␈α∂the␈α∂locations,␈α∂and␈α⊂without␈α∂marking␈α∂the␈α∂stack.␈α⊂In␈α∂this␈α∂implementation␈α∂␈↓αunbind␈↓␈α⊂would␈α∂take
␈↓ ↓H␈↓arguments describing which variables needed restoring.
␈↓ ↓H␈↓%25.17␈↓ εPstack implementation of shallow binding     229%*



␈↓ ↓H␈↓Here's␈α∂a␈α∂sample␈α∂session␈α∂with␈α∂the␈α∂binding␈α⊂mechanism:␈α∂Assume␈α∂␈↓αX␈↓␈α∂and␈α∂␈↓αY␈↓␈α∂are␈α∂currently␈α⊂bound␈α∂to
␈↓ ↓H␈↓␈↓α(A . 1)␈↓ and ␈↓α(B . 2)␈↓ respectively; and assume we wish to evaluate:

␈↓ ↓H␈↓␈↓ βi␈↓α((LAMBDA (X Y) (␈↓λx␈↓α  X Y)) (QUOTE C) (QUOTE D))␈↓.

␈↓ ↓H␈↓␈↓ ∧3We assume SP is in some well-defined state:

␈↓"␈↓ ↓H␈↓
                           part of atom for X         part of atom for Y
␈↓"␈↓ ↓H␈↓
                         ⊂αααααααπααα⊃ ⊂αααπαα      ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"␈↓ ↓H␈↓
  SP                 ~   ~ VALUE ~ #αβ→~ # ~ #→...  ~ VALUE ~ #αβ→~ # ~ #→...
␈↓"␈↓ ↓H␈↓
⊂αααα⊃  εααααααααααααλ   %ααααααα∀ααα$ %αβα∀αα      %ααααααα∀ααα$ %αβα∀αα
␈↓"␈↓ ↓H␈↓
~  #αβα→~*MARK*   #ααβ→⊃                 ↓                          ↓
␈↓"␈↓ ↓H␈↓
%αααα$  εααααααααααααλ ↓            ⊂αααπααα⊃                   ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
        ~ last entry ~ ~            ~ A ~ 1 ~                   ~ B ~ 2 ~
␈↓"␈↓ ↓H␈↓
        εααααααααααααλ ~            %ααα∀ααα$                   %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
             ...
␈↓"␈↓ ↓H␈↓
                       ↓
␈↓"␈↓ ↓H␈↓
        εααααααααααααλ←$
␈↓"␈↓ ↓H␈↓
        ~*MARK*   #ααβ→⊃
␈↓"␈↓ ↓H␈↓
        εααααααααααααλ ↓
␈↓"␈↓ ↓H␈↓
             ...
␈↓ ↓H␈↓␈↓ βπNow we save the currrent value of ␈↓αX␈↓ and the location of its value cell:
␈↓"␈↓ ↓H␈↓
            ⊂αααααααααα→αααααααα→αααααααααα→αααααααααα→α⊃
␈↓"␈↓ ↓H␈↓
  SP     ~  ↑        ~                                  ~
␈↓"␈↓ ↓H␈↓
⊂αααα⊃   εααβααπαααααλ                                  ~
␈↓"␈↓ ↓H␈↓
~ #ααβαα→~  #  ~  #ααβ→ααα⊃         part of atom for X  ↓
␈↓"␈↓ ↓H␈↓
%αααα$   εααααα∀αααααλ    ↓            ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"␈↓ ↓H␈↓
         ~*MARK*  #ααβ→⊃  ~            ~ VALUE ~ #αβ→~ # ~ #→...
␈↓"␈↓ ↓H␈↓
         εαααααααααααλ ↓  ~            %ααααααα∀ααα$ %αβα∀αα
␈↓"␈↓ ↓H␈↓
         ~last entry ~ .  ↓                            ↓
␈↓"␈↓ ↓H␈↓
         εαααααααααααλ .  ~                          ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
              ...      .  %αααααα→ααααααααα→αααααααα→~ A ~ 1 ~
␈↓"␈↓ ↓H␈↓
                                                     %ααα∀ααα$

␈↓ ↓H␈↓A␈αsimilar␈α"push"␈αsaves␈αthe␈αvalue␈αof␈α␈↓αY␈↓.␈αOnce␈αthis␈αis␈αdone␈αwe␈αare␈αfree␈αto␈αbind␈α␈↓αX␈↓␈αand␈α␈↓αY␈↓␈αto␈αthe␈αnew
␈↓ ↓H␈↓values.
␈↓ ↓H␈↓␈↓ βaHere's what we have after rebinding ␈↓αX␈↓ to ␈↓αC␈↓ and ␈↓αY␈↓ to ␈↓αD␈↓:
␈↓"␈↓ ↓H␈↓
            ⊂αααααααααα→αααααααα→αααααααααα→αααααααααα→α⊃
␈↓"␈↓ ↓H␈↓
            ~                                           ~
␈↓"␈↓ ↓H␈↓
  SP     ~  ↑                                           ↓
␈↓"␈↓ ↓H␈↓
⊂αααα⊃   εααβααπαααααλ                                  ~
␈↓"␈↓ ↓H␈↓
~ #ααβαα→~  #  ~  #ααβ→ααα⊃          part of atom for Y ↓
␈↓"␈↓ ↓H␈↓
%αααα$   εαααααβαααααλ    ~            ⊂αααααααπααα⊃ ⊂αααπαα
␈↓"␈↓ ↓H␈↓
    ⊂α←ααβαα#  ~  #ααβ→⊃  ~            ~ VALUE ~ #αβ→~ D ~ #→...
␈↓"␈↓ ↓H␈↓
    ~    εααααα∀αααααλ ~  ~            %ααααααα∀ααα$ %ααα∀αα
␈↓"␈↓ ↓H␈↓
    ↓    ~*MARK*  #→   ↓  ↓
␈↓"␈↓ ↓H␈↓
    ~    εαααααααααααλ ~  ~
␈↓"␈↓ ↓H␈↓
    ~    ~ last entry~ ~  ~
␈↓"␈↓ ↓H␈↓
    ~    εαααααααααααλ ↓  ~                          ⊂αααπααα⊃
␈↓"␈↓ ↓H␈↓
    ↓                  ~  %ααααααααααα→ααααααααααααα→~ B ~ 2 ~
␈↓"␈↓ ↓H␈↓
    ~  ⊂αααπααα⊃       ~       ⊂αααπααα⊃             %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
    %α→~ C ~ #αβ→ ...  %αα→ααα→~ A ~ 1 ~
␈↓"␈↓ ↓H␈↓
       %ααα∀ααα$               %ααα∀ααα$
␈↓"␈↓ ↓H␈↓
    part of atom X
␈↓ ↓H␈↓␈↓↓230  static structure␈↓ (5.17␈↓


␈↓ ↓H␈↓␈↓ β8Then we top off SP to acknowledge a block of saved bindings:
␈↓"␈↓ ↓H␈↓
  SP    ~            ~
␈↓"␈↓ ↓H␈↓
⊂αααα⊃  εααααααααααααλ
␈↓"␈↓ ↓H␈↓
~  #αβα→~*MARK*   #ααβ→⊃
␈↓"␈↓ ↓H␈↓
%αααα$  εααααααααααααλ ~
␈↓"␈↓ ↓H␈↓
        ~  saved  Y  ~ ↓
␈↓"␈↓ ↓H␈↓
        εααααααααααααλ ~
␈↓"␈↓ ↓H␈↓
        ~  saved  X  ~ ↓
␈↓"␈↓ ↓H␈↓
        εααααααααααααλ←$
␈↓"␈↓ ↓H␈↓
        ~*MARK*   #ααβ→⊃
␈↓"␈↓ ↓H␈↓
        εααααααααααααλ ↓
␈↓"␈↓ ↓H␈↓
             ...

␈↓ ↓H␈↓Notice␈αthat␈α
␈↓αX␈↓␈αand␈α
␈↓αY␈↓␈αhave␈αvalues␈α
␈↓αC␈↓␈αand␈α
␈↓αD␈↓␈αrespectively␈αnow␈α
and␈αthe␈α
previous␈αbindings␈α
are␈αsaved
␈↓ ↓H␈↓on␈αthe␈αSP-stack.␈αWe␈αmay␈αnow␈αbegin␈αthe␈αevaluation␈αof␈αthe␈αexpression␈α␈↓α(␈↓λx␈↓α X Y)␈↓␈αassured␈αthat␈αwe␈αwill
␈↓ ↓H␈↓get␈α
the␈αexpected␈α
values␈αfor␈α
␈↓αX␈↓␈α
and␈α␈↓αY␈↓␈α
and␈αassured␈α
that␈αwe␈α
will␈α
be␈αable␈α
to␈αrestore␈α
the␈αprevious␈α
values
␈↓ ↓H␈↓afterwards.␈α
 ␈↓αunbind␈↓␈αsimply␈α
"pops"␈α
entries␈αoff␈α
of␈αthe␈α
top␈α
of␈αSP,␈α
using␈α
the␈αinformation␈α
stored␈αthere␈α
to
␈↓ ↓H␈↓restore the old values; ␈↓αunbind␈↓ stops as soon as it has popped the first block.

␈↓ ↓H␈↓This␈αimplementation␈αworks␈αquite␈αwell␈αfor␈αsimple␈αλ-binding␈αand␈αlookup.␈αChanging␈αenvironments␈αis
␈↓ ↓H␈↓a␈αbit␈αof␈α
work,␈αbut␈αthe␈αaccess␈α
to␈αthe␈αvalues␈α
of␈αvariables␈αis␈αrelatively␈α
rapid,␈αparticularly␈αif␈α
we␈αmake
␈↓ ↓H␈↓sure␈α⊂that␈α⊃the␈α⊂value␈α⊂cell␈α⊃is␈α⊂always␈α⊂stored␈α⊃at␈α⊂a␈α⊂known␈α⊃position␈α⊂relative␈α⊂to␈α⊃the␈α⊂beginning␈α⊃of␈α⊂the
␈↓ ↓H␈↓property␈α
list.␈α
 In␈α
describing␈α
this␈α
implementation␈αwe␈α
have␈α
used␈α
more␈α
representation␈α
than␈αmight␈α
seem
␈↓ ↓H␈↓appropriate.␈α
In␈α∞particular␈α
the␈α
representation␈α∞of␈α
the␈α
value␈α∞cell␈α
as␈α
a␈α∞linked␈α
list␈α∞seems␈α
unnecessarily
␈↓ ↓H␈↓explicit. This was done to illuminate the pointer modifications involved in binding and unbinding.

␈↓ ↓H␈↓How␈αcan␈α
we␈αimplement␈α
the␈αequivalent␈αof␈α
␈↓αFUNARG␈↓␈αin␈α
this␈αnew␈α
binding␈αorganization?␈α Recall␈α
how
␈↓ ↓H␈↓deep␈α
binding␈α
coped.␈α When␈α
we␈α
recognized␈α
an␈αinstance␈α
of␈α
a␈α
functional␈αargument␈α
we␈α
saved␈αa␈α
pointer
␈↓ ↓H␈↓to␈α
the␈α
current␈α
environment.␈α
When␈α
we␈α
␈↓↓applied␈↓␈α
the␈α
functional␈α
argument␈α
we␈α
restored␈α
the␈α
symbol␈α
table
␈↓ ↓H␈↓in␈α
such␈α
a␈α
way␈α
that␈αglobal␈α
variables␈α
were␈α
accessed␈α
in␈αthe␈α
saved␈α
environment␈α
while␈α
local␈αvariables
␈↓ ↓H␈↓were␈α
found␈α
in␈α
the␈α
current␈α
environment.␈α
 We␈α
must␈α
try␈α
to␈α
do␈α
the␈α
same␈α
with␈α
the␈αshallow-binding.␈α
The
␈↓ ↓H␈↓action␈α∞taken␈α∞when␈α∞a␈α∞functional␈α∞argument␈α∂is␈α∞recognized␈α∞is␈α∞quite␈α∞similar␈α∞to␈α∞our␈α∂previous␈α∞solution:
␈↓ ↓H␈↓when␈α
␈↓αfunction␈↓␈αis␈α
seen␈αsave␈α
the␈α
current␈αSP␈α
pointer,␈αrather␈α
than␈α
the␈αcurrent␈α
environment␈αname.␈α
This
␈↓ ↓H␈↓action␈αmanufactures␈αa␈αtriple␈α␈↓α(FUNARG ␈↓<function> <old␈αSP>␈↓α)␈↓.␈αHowever␈αthe␈αaction␈αrequired␈αwhen
␈↓ ↓H␈↓we␈αwish␈αto␈αapply␈αthe␈αfunctional␈αargument␈αis␈αmuch␈αmore␈αcomplicated.␈αBefore,␈αwe␈αjust␈αset␈αup␈αa␈αnew
␈↓ ↓H␈↓access␈α∀chain␈α∪such␈α∀that␈α∪the␈α∀local␈α∪table␈α∀referred␈α∪to␈α∀the␈α∪environment␈α∀saved␈α∪by␈α∀the␈α∪␈↓αFUNARG␈↓
␈↓ ↓H␈↓construction␈α
whenever␈α
a␈α
global␈α
value␈α
was␈α
needed.␈α
 The␈α
main␈α
problem␈α
with␈α
the␈α
shallow-binder␈α
is
␈↓ ↓H␈↓that␈αthe␈αSpecial␈αPushdown␈αList␈αonly␈α
reflects␈αthe␈α␈↓↓incremental␈↓␈αchanges␈αin␈αthe␈α
environments␈αduring
␈↓ ↓H␈↓the␈α∞computation.␈α
To␈α∞retrieve␈α
the␈α∞environment␈α∞current␈α
when␈α∞the␈α
functional␈α∞argument␈α∞was␈α
bound,
␈↓ ↓H␈↓we␈α
must␈α
unwind␈α
SP␈α
back␈αto␈α
the␈α
state␈α
which␈α
was␈α
then␈αoperative;␈α
we␈α
must␈α
also␈α
save␈α
the␈α␈↓↓current␈↓␈α
state
␈↓ ↓H␈↓so␈α∞that␈α∞we␈α∂may␈α∞return␈α∞to␈α∂␈↓↓it␈↓␈α∞when␈α∞finished␈α∂with␈α∞the␈α∞functional␈α∂argument.␈α∞ Now␈α∞for␈α∂more␈α∞detail:
␈↓ ↓H␈↓when we apply the functional argument,

␈↓ ↓H␈↓␈↓α␈↓ βx(FUNARG ␈↓<function> <old SP>␈↓α) ␈↓to     arg␈↓β1␈↓; ... arg␈↓βn␈↓α ,


␈↓ ↓H␈↓we␈α∂will␈α∞first␈α∂rebind␈α∞all␈α∂of␈α∞the␈α∂variables␈α∂found␈α∞by␈α∂the␈α∞old␈α∂SP␈α∞pointer␈α∂to␈α∞those␈α∂old␈α∂values,␈α∞while
␈↓ ↓H␈↓␈↓↓5.17␈↓ εKstack implementation of shallow binding     231␈↓


␈↓ ↓H␈↓saving␈α
the␈α∞current␈α
values␈α∞in␈α
SP.␈α∞Then␈α
we␈α
can␈α∞rebind␈α
the␈α∞λ-variables␈α
(i.e.,␈α∞local␈α
variables)␈α∞of␈α
the
␈↓ ↓H␈↓<function>␈αto␈αarg␈↓β1␈↓␈αthrough␈αarg␈↓βn␈↓.␈αThe␈αenvironment␈αwill␈αthen␈αbe␈αproperly␈αrestored␈αfor␈αevaluation␈αof
␈↓ ↓H␈↓the␈αfunction␈αbody.␈α
When␈αthe␈αevaluation␈αhas␈α
been␈αcompleted␈αwe␈α
restore␈αusing␈α␈↓αunbind␈↓.␈α This␈α
process
␈↓ ↓H␈↓is complex enough to warrant an example:



␈↓ ↓H␈↓␈↓ ∧∀␈↓↓An example of shallow binding and ␈↓αFUNARG␈↓↓␈↓α


␈↓ ↓H␈↓As␈αan␈αexample␈αof␈αthe␈αcomplications␈αinvolved␈αin␈αhandling␈αfunctional␈αarguments␈αin␈αshallow␈α
binding
␈↓ ↓H␈↓we offer the following:

␈↓ ↓H␈↓␈↓↓I␈↓ Assume that ␈↓αx␈↓ initially has value ␈↓α1␈↓ and the SP pointer is at location ␈↓
SP1␈↓,

␈↓ ↓H␈↓␈↓↓II␈↓ then assume that a λ-binding rebinds ␈↓αx␈↓ to ␈↓α2␈↓;

␈↓ ↓H␈↓␈↓↓III␈↓ in this new context, assume a functional argument, ␈↓αg␈↓, is to be bound to a function-variable ␈↓αf␈↓.

␈↓ ↓H␈↓␈↓↓IV-V␈↓␈αAs␈αthe␈αcomputation␈αcontinues␈α␈↓αx␈↓␈αis␈αrebound␈αfirst␈αto␈α␈↓α3␈↓␈αand␈αwithin␈α␈↓↓that␈↓␈αcontext␈αrebound␈αagain
␈↓ ↓H␈↓␈↓ α_to ␈↓α4␈↓.

␈↓ ↓H␈↓␈↓↓VI␈↓␈αFinally␈α␈↓αf␈↓␈αis␈αapplied;␈αthis␈αwill␈αresurrect␈α␈↓αg␈↓␈↓π 113␈↓␈αrequiring␈αa␈αrestoration␈αof␈αthe␈αenvironment␈αcurrent
␈↓ ↓H␈↓␈↓ α_at step ␈↓↓III␈↓.

␈↓ ↓H␈↓Steps ␈↓↓I␈↓ through ␈↓↓V␈↓ would lead to the following sequence.
␈↓"␈↓ ↓H␈↓
                                               F: (FUNARG G #)
␈↓"␈↓ ↓H␈↓
X: 1                    X: 2                   X: 2         ↓
␈↓"␈↓ ↓H␈↓
       εααα∀αααλ               εαααβαααλ            ⊂ααα←ααα$
␈↓"␈↓ ↓H␈↓
SP1 αα→~*MARK* ~ =II=>  SP2 αα→~ X ~ 1 ~ =III=>     ↓
␈↓"␈↓ ↓H␈↓
       εαααπαααλ               αααα∀αααλ        SP2 ∀α→~ X ~ 1 ~
␈↓"␈↓ ↓H␈↓
          ...                  ~*MARK* ~                  ...
␈↓"␈↓ ↓H␈↓
                               εαααπαααλ
␈↓"␈↓ ↓H␈↓
                                  ...
␈↓"␈↓ ↓H␈↓
                        SP1 αα→~  ...  ~


␈↓"␈↓ ↓H␈↓
      X: 3                   X: 4

␈↓"␈↓ ↓H␈↓
=IV=> SP3 αα→~ X ~ 2 ~  =V=> SP4 εαααβαααλ
␈↓"␈↓ ↓H␈↓
             εαααβαααλ        %α→~ X ~ 3 ~
␈↓"␈↓ ↓H␈↓
               . . .             εαααβαααλ
␈↓"␈↓ ↓H␈↓
      SP2 αα→~ X ~ 1 ~             . . .
␈↓"␈↓ ↓H␈↓
             εαααβαααλ
␈↓"␈↓ ↓H␈↓
                ...


␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 113␈↓ From the value cell of ␈↓αf␈↓ as ␈↓α(FUNARG G SP2)␈↓.
␈↓ ↓H␈↓␈↓↓232  static structure␈↓ (5.17␈↓


␈↓ ↓H␈↓Now␈αto␈αapply␈α
the␈αfunctional␈αargument:␈α␈↓α(FUNARG␈α
G␈αSP2)␈↓.␈α This␈α
is␈αaccomplished␈αby␈αtracing␈α
down
␈↓ ↓H␈↓the␈α∩SP␈α∩stack␈α∩with␈α∩a␈α∩pointer␈α∪␈↓
SP*␈↓,␈α∩moving␈α∩from␈α∩␈↓
SP4␈↓␈α∩--the current stack pointer--␈α∩down␈α∪to␈α∩␈↓
SP2␈↓
␈↓ ↓H␈↓--the ␈↓αFUNARG␈↓ pointer--,␈α∩reversing␈α⊃all␈α∩the␈α⊃intervening␈α∩bindings␈α⊃on␈α∩SP␈α⊃and␈α∩putting␈α∩the␈α⊃saved
␈↓ ↓H␈↓values␈α
back␈αinto␈α
the␈α
value cell.␈α The␈α
pattern␈α
of␈αthese␈α
reversals␈α
must␈αbe␈α
saved;␈α
we␈αdo␈α
this␈αby␈α
adding
␈↓ ↓H␈↓a new block above ␈↓
SP4␈↓.

␈↓ ↓H␈↓Thus, steps ␈↓↓VII␈↓ and ␈↓↓VIII␈↓:

␈↓"␈↓ ↓H␈↓
                                        X: 2

␈↓"␈↓ ↓H␈↓
                                              εαααβαααλ
␈↓"␈↓ ↓H␈↓
       X: 3                             SP6 α→~ X ~ 3 ~
␈↓"␈↓ ↓H␈↓
                                                 ...
␈↓"␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"␈↓ ↓H␈↓
       SP5 α→~ X ~ 4 ~                  SP5 α→~ X ~ 4 ~
␈↓"␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"␈↓ ↓H␈↓
                ...                              ...
␈↓"␈↓ ↓H␈↓
       SP4 α→~ X ~ 3 ~                  SP4 α→~ X ~ 3 ~
␈↓"␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"␈↓ ↓H␈↓
                ...                              ...
␈↓"␈↓ ↓H␈↓
=VII=> SP3 α→~ X ~ 2 ~ ←αα SP*  =VIII=> SP3 α→~ X ~ 2 ~
␈↓"␈↓ ↓H␈↓
                ...                              ...
␈↓"␈↓ ↓H␈↓
             εαααβαααλ                        εαααβαααλ
␈↓"␈↓ ↓H␈↓
       SP2 α→~ X ~ 1 ~                  SP2 α→~ X ~ 1 ~ ←αα SP*
␈↓"␈↓ ↓H␈↓
                ...                              . . .


␈↓ ↓H␈↓Now␈α
we␈α
are␈α
in␈α
a␈αposition␈α
to␈α
evaluate␈α
the␈α
call␈αon␈α
␈↓αg␈↓;␈α
when␈α
we␈α
are␈αfinished␈α
with␈α
␈↓αg␈↓␈α
we␈α
will␈α
use␈αthe
␈↓ ↓H␈↓unbinding␈α⊂mechanism␈α⊂to␈α⊂reinstate␈α⊂the␈α⊂world␈α⊂as␈α⊂it␈α⊂existed␈α⊂at␈α⊂␈↓
SP4␈↓.␈α⊂This␈α⊂process␈α⊂will␈α⊂restore␈α∂the
␈↓ ↓H␈↓value cells using the areas of the stack between ␈↓
SP6␈↓ and ␈↓
SP4␈↓.

␈↓ ↓H␈↓This␈α∞implementation␈α∞of␈α∞shallow␈α∞binding␈α∞obviates␈α∞the␈α∞symbol␈α∞table␈α∞search␈α∞while␈α∞incurring␈α
added
␈↓ ↓H␈↓expense␈α
in␈αbinding␈α
and␈α
unbinding.␈α Functional␈α
arguments␈α
are␈αmore␈α
difficult␈α
since␈αthere␈α
is␈αonly␈α
one
␈↓ ↓H␈↓symbol␈α∂table,␈α∞not␈α∂the␈α∂stack␈α∞of␈α∂tables␈α∂implicit␈α∞in␈α∂the␈α∂previous␈α∞incarnation.␈α∂True,␈α∂the␈α∞information
␈↓ ↓H␈↓necessary to recover an environment is present in SP, but it is expensive to retrieve it.

␈↓ ↓H␈↓Though␈αthe␈αstack␈αimplementation␈α
of␈αshallow␈αbinding␈α␈↓↓will␈↓␈α
perform␈αfor␈αfunctional␈αarguments,␈αit␈α
will
␈↓ ↓H␈↓involve␈α
even␈αmore␈α
complexity␈α
if␈αwe␈α
wish␈αto␈α
handle␈α
functional␈αvalues.␈α
 The␈α
difficulty␈αis␈α
the␈αsame␈α
as
␈↓ ↓H␈↓that␈α∂for␈α∂the␈α∂stack␈α⊂implementation␈α∂of␈α∂deep␈α∂binding:␈α∂the␈α⊂␈↓αFUNARG␈↓␈α∂will␈α∂point␈α∂"up"␈α∂the␈α⊂SP␈α∂stack
␈↓ ↓H␈↓rather␈αthan␈α"down".␈α A␈αstraightforward␈αapplication␈αof␈αthe␈αtechnique␈αused␈αfor␈αfunctional␈αarguments
␈↓ ↓H␈↓will␈α∞not␈α∞work.␈α∞At␈α∞the␈α∞time␈α∞we␈α∞wished␈α∞to␈α∞apply␈α∞the␈α∞functional␈α∞value␈α∞its␈α∞saved␈α∞SP-pointer␈α∞will␈α
be
␈↓ ↓H␈↓pointing␈αinto␈α
a␈αsection␈α
of␈αthe␈α
SP␈αstack␈α
which␈αno␈α
longer␈αreflects␈α
the␈αproper␈α
state.␈αFor␈α
when␈αwe␈α
leave
␈↓ ↓H␈↓the␈αenvironment␈αwhich␈αcreated␈αthe␈αfunctional␈αvalue␈αthe␈αcurrent␈αunbinding␈αmechanism␈αwill␈αcut␈αthe
␈↓ ↓H␈↓stack back to the point which existed when we entered that environment.

␈↓ ↓H␈↓A␈α∀difficulty␈α∀arises␈α∃even␈α∀in␈α∀the␈α∀case␈α∃of␈α∀functional␈α∀arguments:␈α∀since␈α∃a␈α∀␈↓↓copy␈↓␈α∀of␈α∃the␈α∀binding
␈↓ ↓H␈↓environment␈αis␈αbeing␈αgenerated␈αin␈αthe␈αunwinding␈αprocess,␈αchanges␈αin␈α␈↓↓that␈↓␈αenvironment␈αwill␈αnot␈αbe
␈↓ ↓H␈↓reflected␈α∩when␈α⊃we␈α∩restore␈α⊃the␈α∩activation␈α⊃environment.␈α∩ The␈α⊃real␈α∩difficulty␈α⊃is␈α∩in␈α∩attempting␈α⊃to
␈↓ ↓H␈↓␈↓↓5.17␈↓ εKstack implementation of shallow binding     233␈↓


␈↓ ↓H␈↓represent␈αthe␈αtree-like␈α
structure␈αof␈αenvironments␈α
by␈αusing␈αthe␈α
essentially␈αlinear␈αrepresentation␈α
of␈αa
␈↓ ↓H␈↓stack.



␈↓ ↓H␈↓␈↓ ∧:␈↓↓5.18  Strategies for LISP implementation␈↓


␈↓ ↓H␈↓ref count, B-W, W-C.M., RG

␈↓ ↓H␈↓The␈α⊂discussion␈α⊂of␈α⊂the␈α⊂last␈α∂three␈α⊂sections␈α⊂should␈α⊂be␈α⊂related␈α∂to␈α⊂the␈α⊂earlier␈α⊂discussion␈α⊂of␈α∂binding
␈↓ ↓H␈↓strategies,␈αWeizenbaum␈α
environments,␈αfunctional␈α
arguments␈αand␈α
the␈αnon-recursive␈αevaluators.␈α
 Our
␈↓ ↓H␈↓mapping␈α
of␈αthe␈α
binding␈α
implementations␈α(shallow␈α
or␈αdeep)␈α
onto␈α
a␈αstack␈α
is␈αsufficient␈α
for␈α
the␈αgreat
␈↓ ↓H␈↓majority␈αof␈αcurrent␈αLISP␈αprograms.␈αHowever␈α
as␈αthe␈αLISP␈αcommunity␈αexplores␈αnew␈αways␈α
of␈αusing
␈↓ ↓H␈↓the␈αlanguage,␈αthey␈αhave␈αbegun␈αto␈αexpect␈αthat␈αthe␈αfull␈αpower␈αof␈αfunctional␈αvalues␈αbe␈αavailable;␈αand
␈↓ ↓H␈↓have␈α
proposed␈αextensions␈α
in␈αthe␈α
LISP␈αcontrol␈α
structure␈αto␈α
allow␈αeven␈α
non-recursive␈α
control.␈αSince
␈↓ ↓H␈↓these␈αtopics␈αare␈αof␈αcurrent␈αresearch␈αinterest␈αit␈αis␈αnot␈αclear␈αhow␈αlasting␈αtheir␈αimpact␈αwill␈αbe.␈αWe␈αwill
␈↓ ↓H␈↓sketch␈α∂a␈α∂few␈α⊂of␈α∂the␈α∂ideas␈α⊂involved␈α∂and␈α∂indicate␈α⊂how␈α∂the␈α∂techniques␈α⊂we␈α∂have␈α∂dsicussed␈α⊂in␈α∂this
␈↓ ↓H␈↓chapter can be applied.

␈↓ ↓H␈↓We␈αbegin␈αwith␈αa␈αreview␈αof␈αthe␈αinitial␈αevaluator␈αof␈αSection 4.5.␈αIn␈αthat␈αimplementation␈αwe␈αkept␈αthe
␈↓ ↓H␈↓symbol␈αtables␈αavailable␈αby␈αrepresenting␈αthem␈αas␈αlist-structure␈αand␈αtherefore␈αthey␈αbecame␈αa␈αgarbage
␈↓ ↓H␈↓collectable␈α∩commodity.␈α∩As␈α∩long␈α∩as␈α∩a␈α∪␈↓αFUNARG␈↓␈α∩construct␈α∩accessed␈α∩a␈α∩table␈α∩then␈α∩the␈α∪table␈α∩was
␈↓ ↓H␈↓retained.␈αEssentially␈αwe␈αhad␈αremoved␈αthe␈αstack-like␈αbehavior␈αof␈αsymbol-table␈αaccesses␈αwhich␈αoccurs
␈↓ ↓H␈↓most␈α∂of␈α∂the␈α∂time␈α∞and␈α∂replaced␈α∂it␈α∂with␈α∂a␈α∞general␈α∂scheme␈α∂which␈α∂works␈α∞for␈α∂all␈α∂cases␈α∂but␈α∂incurs␈α∞a
␈↓ ↓H␈↓significant␈α⊂overhead␈α⊂in␈α⊂even␈α⊂the␈α⊂most␈α⊂simple␈α∂of␈α⊂function␈α⊂calls.␈α⊂We␈α⊂would␈α⊂like␈α⊂an␈α∂intermediate
␈↓ ↓H␈↓solution:␈αone␈αwhich␈αworks␈αfor␈αall␈αcases␈αand␈αminimizes␈αoverhead␈αin␈αthe␈αtypical␈αcall.␈αSuch␈α
a␈αscheme
␈↓ ↓H␈↓can␈αindeed␈α
be␈αimplemented.␈α Recall␈α
our␈αdiscussion␈α
of␈αgarbage␈αcollection␈α
in␈αSection 5.13.␈α
There␈αwe
␈↓ ↓H␈↓said␈αthat␈α
a␈αgarbage␈αcollector␈α
was␈αused␈αin␈α
LISP␈αsince␈αthe␈α
interrelationships␈αwhich␈αwe␈α
generated␈αin
␈↓ ↓H␈↓the␈α∞data␈α∞structure␈α∞manipulations␈α
were␈α∞sufficiently␈α∞intertwined␈α∞that␈α∞it␈α
was␈α∞not␈α∞possible␈α∞to␈α∞use␈α
less
␈↓ ↓H␈↓sophisticated methods to determine whether a structure was still active.

␈↓ ↓H␈↓Now␈α∪local␈α∪symbol␈α∪tables␈α∪␈↓↓are␈↓␈α∪data␈α∪structures;␈α∪the␈α∪discussion␈α∪of␈α∪Weizenbaum␈α∪environments␈α∩in
␈↓ ↓H␈↓Section 4.8␈α
should␈α
have␈α
convinced␈αyou␈α
of␈α
that.␈α
 They␈α
are␈αchained␈α
together␈α
in␈α
a␈αmanner␈α
reminiscent
␈↓ ↓H␈↓of␈αthat␈α
of␈αour␈αimplementation␈α
of␈αS-exprs;␈α
indeed␈αas␈αwe␈α
have␈αjust␈α
mentioned␈αLISP's␈αattitude␈α
toward
␈↓ ↓H␈↓management␈αof␈αsuch␈α
tables␈αwas␈αto␈αgarbage␈α
collect␈αthem.␈αHowever␈αthe␈α
␈↓↓behavior␈↓␈αof␈αtables␈αduring␈α
the
␈↓ ↓H␈↓execution␈α
of␈α
a␈α
program␈αis␈α
much␈α
less␈α
complex␈αthan␈α
that␈α
of␈α
arbitrary␈αlist␈α
structure.␈α
Again␈α
as␈αwe␈α
have
␈↓ ↓H␈↓just␈αseen␈α
the␈αbehavior␈α
is␈αquite␈α
rational␈αand␈α
predictable␈αexcept␈α
for␈αprocedure-valued␈α
variables.␈α A
␈↓ ↓H␈↓solution␈α
giving␈αa␈α
reasonable␈αimplementation␈α
based␈αon␈α
the␈αalternative␈α
storage␈α
management␈αscheme
␈↓ ↓H␈↓of␈αreference␈αcounters,␈αwhich␈αwe␈αdescribed␈αon␈αpage 220,␈αis␈αdescribed␈αin␈αa␈αpaper␈αby␈αD.␈αBobrow␈αand
␈↓ ↓H␈↓B.␈α
Wegbreit.␈α∞ The␈α
effect␈α
of␈α∞their␈α
representation␈α∞of␈α
the␈α
control␈α∞for␈α
LISP␈α
is␈α∞that␈α
little␈α∞overhead␈α
is
␈↓ ↓H␈↓extracted␈αif␈αthe␈αprogram␈αdoes␈αnot␈αuse␈αthe␈αmore␈αexotic␈αfeatures:␈αa␈αstack-like␈αdevice␈αresults.␈αA␈αlarger
␈↓ ↓H␈↓toll is paid for use of more general control regimes.

␈↓ ↓H␈↓**loss on complier optimization***
␈↓ ↓H␈↓␈↓↓234  static structure␈↓ '5.18␈↓


␈↓ ↓H␈↓*** W CM and RG on shallow***

␈↓ ↓H␈↓It␈α
␈↓↓is␈↓␈α
possible␈α
to␈α
consider␈α
combining␈α
the␈α
use␈α
of␈α
the␈α
value␈α
cell␈α
with␈α
either␈α
shallow␈α
and␈αdeep␈α
strategies.
␈↓ ↓H␈↓We␈αhave␈α␈↓↓both␈↓␈αa␈αvalue␈αcell␈αand␈αeither␈αname-value␈αtrees␈αor␈αshallow␈αbound␈αp-lists.␈αWe␈αwill␈αtry␈αto␈αuse
␈↓ ↓H␈↓the␈α∞value cell␈α∂as␈α∞much␈α∞as␈α∂possible.␈α∞ We␈α∞associate␈α∂an␈α∞extra␈α∞piece␈α∂of␈α∞information␈α∞with␈α∂each␈α∞value
␈↓ ↓H␈↓attached␈α
to␈α∞any␈α
value cell,␈α∞telling␈α
the␈α∞binding␈α
time␈α∞of␈α
that␈α∞variable.␈α
 We␈α∞have␈α
a␈α∞global␈α
indicator
␈↓ ↓H␈↓telling␈αthe␈αcurrent␈αcontext␈αwhich␈αwe␈αare␈αusing:␈αE␈↓βcurrent␈↓,␈αsay.␈αWhen␈αwe␈αwant␈αthe␈αvalue␈αof␈αa␈αvariable,
␈↓ ↓H␈↓we␈α
first␈αgo␈α
to␈α
the␈αaugmented␈α
value cell;␈α
if␈αthe␈α
binding␈α
time␈αindication␈α
is␈α
that␈αof␈α
E␈↓βcurrent␈↓,␈α
then␈αthe
␈↓ ↓H␈↓value␈αis␈αcorrect␈αand␈αwe␈αtake␈αit.␈α If␈αthe␈αindicators␈αdisagree,␈αwe␈αuse␈αthe␈αfull␈α␈↓αlookup␈↓␈αstrategy;␈αwhen␈αwe
␈↓ ↓H␈↓find␈αthe␈αvariable␈αwe␈α
update␈αthe␈αvalue cell␈αand␈αchange␈α
the␈αbinding␈αtime␈αindicator␈αto␈α
E␈↓βcurrent␈↓.␈αThis
␈↓ ↓H␈↓way␈αwe␈αonly␈αsearch␈αthe␈αstacks␈αfor␈αthe␈αfirst␈α
access␈αto␈αa␈αvariable;␈αafter␈αthat␈αwe␈αcan␈αjustifiably␈αuse␈α
the
␈↓ ↓H␈↓value cell until we change context again.



␈↓ ↓H␈↓␈↓ ¬s␈↓↓5.19  Epilogue␈↓


␈↓ ↓H␈↓␈↓ βB␈↓↓Subtitled: But my Fortran program doesn't do all this crap!␈↓ 

␈↓ ↓H␈↓It␈αis␈αquite␈αtrue␈αthat␈αa␈αrunning␈αFortran,␈αPL/1,␈αor␈αAlgol␈αprogram␈αis␈αfar␈αless␈αcomplicated␈αas␈αfar␈αas␈αits
␈↓ ↓H␈↓symbol␈α⊂accessing␈α⊂mechanisms␈α⊂are␈α⊂concerned.␈α⊂ In␈α∂Fortran␈α⊂there␈α⊂is␈α⊂a␈α⊂simple␈α⊂relationship␈α∂between
␈↓ ↓H␈↓variables␈α∂and␈α∂memory␈α∂locations␈α∂which␈α∂will␈α∂contain␈α∂their␈α∂values;␈α∂a␈α∂Fortran␈α∂evaluator␈α∂can␈α∂assign
␈↓ ↓H␈↓fixed␈α∂locations␈α∂to␈α∂the␈α⊂variables␈α∂in␈α∂a␈α∂program.␈α∂ In␈α⊂Algol,␈α∂there␈α∂is␈α∂a␈α∂simple␈α⊂relationship␈α∂between
␈↓ ↓H␈↓variables␈αand␈αpositions␈αin␈αthe␈αrun-time␈αstack;␈αan␈αAlgol␈αevaluator␈αcannot␈αassign␈αfixed␈αlocations,␈αbut
␈↓ ↓H␈↓it␈α
can␈α
replace␈αthe␈α
variable␈α
lookup␈αwith␈α
a␈α
simple␈αaddress␈α
calculation.␈α
This␈αis␈α
partly␈α
due␈α
to␈αAlgol's
␈↓ ↓H␈↓use of static binding and partly due to its restrictions on procedure-valued variables.

␈↓ ↓H␈↓In␈α
LISP,␈α
both␈αthe␈α
quality␈α
and␈α
the␈αquantity␈α
of␈α
variables␈α
can␈αchange.␈α
 Arbitrary␈α
properties␈α
can␈αbe
␈↓ ↓H␈↓associated␈α⊗with␈α⊗atoms␈α∃at␈α⊗run-time.␈α⊗ Indeed,␈α∃the␈α⊗symbol␈α⊗table␈α∃mechanism␈α⊗of␈α⊗LISP␈α⊗is␈α∃more
␈↓ ↓H␈↓reminiscent␈α∞of␈α∂that␈α∞associated␈α∞with␈α∂the␈α∞Fortran␈α∞or␈α∂Algol␈α∞compiler.␈α∞ For␈α∂these␈α∞languages␈α∞it␈α∂is␈α∞the
␈↓ ↓H␈↓compiler␈α∞which␈α∞performs␈α∞the␈α
mapping␈α∞from␈α∞source␈α∞language␈α∞to␈α
running␈α∞machine␈α∞code.␈α∞ It␈α∞is␈α
the
␈↓ ↓H␈↓compiler's␈αresponsibility␈αto␈α
discover␈αthe␈αproperties␈α
associated␈αwith␈αeach␈α
variable.␈α The␈αcompiler␈α
can
␈↓ ↓H␈↓do␈αthis␈αbecause␈αthe␈αsemantics␈αof␈αthe␈αlanguage␈αis␈αsuch␈αthat␈αat␈αcompile␈αtime␈αall,␈αor␈αalmost␈αall,␈αof␈αthe
␈↓ ↓H␈↓properties␈αof␈αthe␈αvariables␈αare␈αknown.␈α This␈αis␈αnot␈αtrue␈αfor␈αLISP.␈α In␈αgeneral␈αyou␈αcannot␈αtell␈αuntil
␈↓ ↓H␈↓run␈αtime␈αwhat␈αthe␈αattributes␈αof␈αa␈αparticular␈αatom␈αare.␈α The␈αsituation␈αis␈αreally␈αeven␈αworse␈αthan␈α
this.
␈↓ ↓H␈↓Since␈α∂programs␈α∂and␈α⊂data␈α∂are␈α∂indistinguishable,␈α⊂we␈α∂can␈α∂construct␈α∂a␈α⊂list␈α∂using␈α∂the␈α⊂data␈α∂structure
␈↓ ↓H␈↓facilities and the turn right around and evaluate that list as a representation of a LISP expression.

␈↓ ↓H␈↓However,␈α∞a␈α∂large␈α∞majority␈α∂of␈α∞LISP␈α∞computations␈α∂fall␈α∞into␈α∂a␈α∞much␈α∞more␈α∂disciplined␈α∞set,␈α∂and␈α∞for
␈↓ ↓H␈↓those␈α
computations,␈αit␈α
is␈α
reasonable␈αto␈α
think␈α
about␈αapplying␈α
some␈α
of␈αthe␈α
ideas␈α
available␈αfor␈α
Fortran
␈↓ ↓H␈↓or␈α
Algol␈αtranslators.␈α
That␈αis␈α
we␈αdon't␈α
use␈αall␈α
of␈α
the␈αgenerality␈α
available␈αin␈α
the␈αlanguage␈α
and␈αwe␈α
can
␈↓ ↓H␈↓therefore␈α∞reduce␈α∞some␈α∞of␈α∞the␈α
variable␈α∞look␈α∞up,␈α∞for␈α∞example.␈α
 For␈α∞these␈α∞kinds␈α∞of␈α∞computations,␈α
it
␈↓ ↓H␈↓might␈α
be␈α
appropriate␈α
to␈α
compile␈α
out␈α
the␈α
uneeded␈α
generality.␈α
 There␈α
are␈α
LISP␈α
compilers,␈α
typically
␈↓ ↓H␈↓written␈αin␈αLISP.␈αThey␈αcan␈αmake␈αmany␈αdecisions␈αat␈αcompile␈αtime␈αabout␈αthe␈αproperties␈αof␈αvariables,
␈↓ ↓H␈↓␈↓↓5.19␈↓ 
βEpilogue     235␈↓


␈↓ ↓H␈↓but␈α
in␈α
general␈αthe␈α
compiled␈α
code␈αwill␈α
be␈α
interspersed␈αwith␈α
calls␈α
on␈α␈↓αeval␈↓␈α
because␈α
the␈α
compiler␈αjust
␈↓ ↓H␈↓doesn't␈α∩know␈α∩what␈α∩to␈α∩do.␈α∩ This␈α∩implies␈α∩that␈α∩compiled␈α∩and␈α∩interpreted␈α∩code␈α∩must␈α∩be␈α∪able␈α∩to
␈↓ ↓H␈↓communicate␈α
with␈α
each␈αother.␈α
 A␈α
piece␈α
of␈αcompiled␈α
code␈α
can␈α
call␈αa␈α
λ-expression␈α
or␈αconversely.␈α
 The
␈↓ ↓H␈↓execution␈α∞of␈α
the␈α∞program␈α∞should␈α
be␈α∞totally␈α∞transparent␈α
as␈α∞to␈α∞whether␈α
any,␈α∞or␈α∞all,␈α
or␈α∞none␈α∞of␈α
the
␈↓ ↓H␈↓functions␈αare␈αcompiled.␈α This␈αmeans␈αthat␈αthe␈αcalling␈αsequences␈αfor␈αboth␈αkinds␈αof␈αfunctions␈αmust␈αbe
␈↓ ↓H␈↓compatible.␈αLess␈αobvious␈αand␈αby␈αfar␈αmore␈αtroublesome,␈αis␈αthe␈αcommunication␈αof␈αthe␈αvalues␈αof␈αfree
␈↓ ↓H␈↓variables.␈α≠The␈α≠next␈α≤chapter␈α≠of␈α≠the␈α≠text␈α≤discusses␈α≠the␈α≠run-time␈α≠behavior␈α≤required␈α≠for
␈↓ ↓H␈↓implementations of LISP-like languages and will include a discussion of LISP compilers.
␈↓ ↓H␈↓␈↓↓236  Projects␈↓ C6.␈↓


␈↓ ↓H␈↓␈↓ ¬␈␈↓↓SECTION 6

␈↓ ↓H␈↓↓␈↓ εPROJECTS␈↓




␈↓ ↓H␈↓This␈α
chapter␈α
consists␈α
of␈αa␈α
set␈α
of␈α
non-trivial␈αprojects␈α
which␈α
either␈α
apply␈αLISP␈α
or␈α
extend␈α
LISP␈αby
␈↓ ↓H␈↓adding new language features.



␈↓ ↓H␈↓␈↓ ¬<␈↓↓6.1  Extensions to ␈↓αeval␈↓↓␈↓α


␈↓ ↓H␈↓This␈α!next␈α!extension␈α to␈α!␈↓αeval␈↓␈α!was␈α!derived␈α from␈α!the␈α!syntax␈α!of␈α MUDDLE[Mud 75],
␈↓ ↓H␈↓CONNIVER[Con 73],␈α_and␈α_MICRO-PLANNER[Mic 71].␈α_ We␈α↔have␈α_seen␈α_that␈α_LISP␈α↔calling
␈↓ ↓H␈↓sequences␈α∩are␈α⊃of␈α∩two␈α∩varieties:␈α⊃either␈α∩evaluate␈α⊃␈↓↓all␈↓␈α∩of␈α∩the␈α⊃arguments;␈α∩or␈α⊃evaluate␈α∩␈↓↓none␈↓␈α∩of␈α⊃the
␈↓ ↓H␈↓arguments.

␈↓ ↓H␈↓In␈α∞an␈α
attempt␈α∞to␈α
generalize␈α∞this␈α
regime␈α∞we␈α
might␈α∞allow␈α
the␈α∞evaluation␈α
of␈α∞some␈α
of␈α∞the␈α
arguments
␈↓ ↓H␈↓and␈α⊂enlarge␈α⊂on␈α⊂the␈α⊂domain␈α⊂of␈α⊂objects␈α⊂which␈α∂can␈α⊂appear␈α⊂in␈α⊂the␈α⊂list␈α⊂of␈α⊂λ-variables.␈α⊂ We␈α∂might
␈↓ ↓H␈↓partition␈α∩the␈α∩formal␈α∪parameters␈α∩into␈α∩required␈α∩parameters,␈α∪optional␈α∩parameters,␈α∩and␈α∪an␈α∩excess
␈↓ ↓H␈↓collector␈αto␈αhandle␈αany␈αactual␈αparameters␈α
left␈αover.␈α Required␈αparameters␈α␈↓↓must␈↓␈αhave␈α
corresponding
␈↓ ↓H␈↓actual␈αparameters;␈αoptional␈αactual␈αparameters␈αare␈αused␈αif␈αpresent,␈αotherwise␈αdeclared␈αdefault␈αvalues
␈↓ ↓H␈↓are␈α⊂used.␈α⊃ If␈α⊂there␈α⊃are␈α⊂more␈α⊂actual␈α⊃parameters␈α⊂than␈α⊃the␈α⊂formals␈α⊂encompassed␈α⊃by␈α⊂the␈α⊃first␈α⊂two
␈↓ ↓H␈↓classes, then they are associated with the excess collector.

␈↓ ↓H␈↓To be more precise consider the following possible BNF equations:

␈↓ ↓H␈↓<varlist>␈↓ βλ::=[<required> <optional> <excess>]

␈↓ ↓H␈↓<required>␈↓ βλ::= <par>; ...;<par> | ␈↓ε␈↓ ␈↓π 114␈↓

␈↓ ↓H␈↓<optional>␈↓ βλ::= "optional" <opn>; ...; <opn> | ␈↓ε␈↓

␈↓ ↓H␈↓<excess>␈↓ βλ::= "excess" <par> | ␈↓ε␈↓

␈↓ ↓H␈↓<par>␈↓ βλ::= <variable> | ␈↓λ`␈↓<variable>

␈↓ ↓H␈↓<opn>␈↓ βλ::= <par> | <par> ← <form>



␈↓ ↓H␈↓________________
␈↓ ↓H␈↓␈↓π 114␈↓ The symbol "␈↓ε␈↓" stands for the empty string.
␈↓ ↓H␈↓␈↓↓6.1␈↓ 	εExtensions to ␈↓αeval␈↓↓     237␈↓α



␈↓ ↓H␈↓The associated semantics are as follows:

␈↓ ↓H␈↓␈↓↓1.␈↓ The formal parameters are to be bound to the actual parameters from left to right as usual.

␈↓ ↓H␈↓␈↓↓2.␈↓␈α⊂There␈α⊃must␈α⊂be␈α⊂an␈α⊃actual␈α⊂parameter␈α⊃for␈α⊂each␈α⊂required␈α⊃parameter,␈α⊂and␈α⊂if␈α⊃there␈α⊂is␈α⊃no␈α⊂excess
␈↓ ↓H␈↓␈↓ αhcollector␈α
there␈α
may␈α
not␈α
be␈α
more␈α
actual␈α
parameters␈α
than␈α
formals.␈α
(There␈α
may␈α
be␈α
fewer␈α
if
␈↓ ↓H␈↓␈↓ αhwe have optionals.)

␈↓ ↓H␈↓␈↓↓3.␈↓␈α⊃If␈α⊃a␈α⊃<variable>␈α⊃in␈α⊃a␈α⊃formal␈α⊃parameter␈α⊃is␈α⊃preceded␈α⊃by␈α⊃a␈α⊃"␈↓λ`␈↓",␈α⊃then␈α⊃the␈α⊃corresponding␈α⊃actual
␈↓ ↓H␈↓␈↓ αhparameter is ␈↓↓not␈↓ evaluated. This is just the ␈↓αquote␈↓-ing ␈↓αread␈↓ macro.

␈↓ ↓H␈↓␈↓↓4.␈↓␈αWe␈αmight␈αrun␈αout␈αof␈αactual␈αparameters␈α
while␈αbinding␈αthe␈αoptionals.␈α If␈αwe␈αdo,␈αthen␈αwe␈α
look␈αat
␈↓ ↓H␈↓␈↓ αhthe␈αremaining␈αformal␈αoptionals.␈α
 If␈αa␈αformal␈αparameter␈αis␈α
simply␈αa␈α<par>␈αthen␈αwe␈α
bind
␈↓ ↓H␈↓␈↓ αhit␈α⊃to␈α⊂␈↓α( )␈↓;␈α⊃if␈α⊂a␈α⊃formal␈α⊂is␈α⊃␈↓λ`␈↓<variable>␈α⊂←␈α⊃<form>␈α⊂then␈α⊃we␈α⊂bind␈α⊃the␈α⊂<variable>␈α⊃to␈α⊂the
␈↓ ↓H␈↓␈↓ αh<form>;␈αand␈αif␈αthe␈αformal␈αis␈α<variable>␈α←␈α<form>,␈αwe␈αbind␈α<variable>␈αto␈αthe␈αvalue␈αof
␈↓ ↓H␈↓␈↓ αh<form>,␈αwhere␈αthe␈αevaluation␈αis␈αto␈αtake␈αplace␈α␈↓↓after␈↓␈αthe␈αrequired␈αparameters␈αhave␈αbeen
␈↓ ↓H␈↓␈↓ αhbound.

␈↓ ↓H␈↓␈↓↓5.␈↓␈αFinally,␈αthe␈αexcess␈αcollector␈αis␈αbound␈αto␈αa␈αlist␈αof␈αany␈αremaining␈αactual␈αparameters␈αin␈αthe␈αobvious
␈↓ ↓H␈↓␈↓ αhway:␈α⊂if␈α⊂<par>␈α⊂is␈α⊃<variable>␈α⊂then␈α⊂using␈α⊂the␈α⊂calling␈α⊃environment,␈α⊂form␈α⊂a␈α⊂list␈α⊃of␈α⊂the
␈↓ ↓H␈↓␈↓ αhvalues␈α
of␈α
the␈α
remaining␈α
arguments;␈α
if␈α
it␈α
is␈α
␈↓λ`␈↓<variable>,␈α
bind␈α
<variable>␈α
to␈α∞the␈α
actual
␈↓ ↓H␈↓␈↓ αhlist. If there is no excess, bind to ␈↓αNIL␈↓.

␈↓ ↓H␈↓We␈α∃will␈α∃also␈α∃extend␈α⊗␈↓αprog␈↓-variables␈α∃slightly,␈α∃allowing␈α∃them␈α⊗to␈α∃be␈α∃initialized␈α∃explicitly.␈α⊗If␈α∃a
␈↓ ↓H␈↓␈↓αprog␈↓-variable␈α∞is␈α
atomic,␈α∞intialize␈α
it␈α∞to␈α
␈↓α( )␈↓,␈α∞as␈α
usual.␈α∞If␈α
it␈α∞is␈α
of␈α∞the␈α
form␈α∞<variable> ← <form>␈α
then
␈↓ ↓H␈↓initialize it to the value of the <form>.

␈↓ ↓H␈↓Here are some examples:

␈↓ ↓H␈↓1. In the initialization of ␈↓αlength␈↓ on page 136, we could write: ␈↓α... prog[[l ← x; c ← 0] ....␈↓

␈↓ ↓H␈↓2. ␈↓αlist␈↓ could now be defined as: ␈↓αλ[[␈↓"excess"␈↓α x]x]␈↓.

␈↓ ↓H␈↓3. Consider the following definition:

␈↓ ↓H␈↓␈↓ αX␈↓αbaz <= λ[␈↓ βX[x;␈↓λ`␈↓αy;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[y];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[z];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[u];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[v] ].
␈↓ ↓H␈↓␈↓↓238  Projects␈↓ 76.1␈↓


␈↓ ↓H␈↓α␈↓Then a call of:

␈↓ ↓H␈↓␈↓ αa␈↓αeval[(BAZ 2 (CAR (QUOTE (X Y))) 4 5 6 7 (CAR (QUOTE (A . B))));NIL]␈↓

␈↓ ↓H␈↓would print:␈↓α␈↓ βX␈↓ βh2
␈↓ ↓H␈↓α␈↓ αX␈↓ βX(CAR(QUOTE (X Y)))
␈↓ ↓H␈↓α␈↓ αX␈↓ βX4
␈↓ ↓H␈↓α␈↓ αX␈↓ βX5
␈↓ ↓H␈↓α␈↓ αX␈↓ βX(6 7 A)␈↓

␈↓ ↓H␈↓and return value: ␈↓α(6 7 A)␈↓.

␈↓ ↓H␈↓Similarly, defining:

␈↓ ↓H␈↓␈↓ αX␈↓αfii <= λ[␈↓ βX[x;y;␈↓"optional"␈↓α z; u ← 0; ␈↓"excess"␈↓α v]
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[x];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[y];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[z];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[u];
␈↓ ↓H␈↓α␈↓ αX␈↓ βXprint[v]].

␈↓ ↓H␈↓α␈↓and calling: ␈↓ ∧D␈↓αeval[(FII 2 (CAR (QUOTE (X Y)));NIL]␈↓
␈↓ ↓H␈↓αprints:␈↓ αX␈↓ βX2
␈↓ ↓H␈↓α␈↓ αX␈↓ βXX
␈↓ ↓H␈↓α␈↓ αX␈↓ βXNIL
␈↓ ↓H␈↓α␈↓ αX␈↓ βX0
␈↓ ↓H␈↓α␈↓ αX␈↓ βXNIL.



␈↓ ↓H␈↓␈↓ ε⊃␈↓↓Problems␈↓


␈↓ ↓H␈↓Design simple S-expr representations of these proposed constructs.  Make these extensions to ␈↓αeval␈↓.



␈↓ ↓H␈↓␈↓ ¬M␈↓↓6.2  Pretty-printing␈↓


␈↓ ↓H␈↓also read macros and meta brackets
␈↓ ↓H␈↓␈↓↓6.3␈↓ 	hData Bases     239␈↓


␈↓ ↓H␈↓␈↓ ¬l␈↓↓6.3  Data Bases␈↓




␈↓ ↓H␈↓␈↓ ¬λ␈↓↓6.4  Syntax-directed processes␈↓


␈↓ ↓H␈↓We␈α∀should␈α∀now␈α∪have␈α∀sufficient␈α∀background␈α∀to␈α∪examine␈α∀reasonably␈α∀complex␈α∀and␈α∪non-trivial
␈↓ ↓H␈↓problems.␈α∪ This␈α∩project␈α∪is␈α∩an␈α∪introduction␈α∩to␈α∪the␈α∩very␈α∪important␈α∩area␈α∪called␈α∩␈↓↓syntax-directed
␈↓ ↓H␈↓↓processes␈↓.␈α≠ Syntax-directed␈α≤techniques␈α≠are␈α≤used␈α≠extensively␈α≤in␈α≠compiler␈α≤construction␈α≠(see
␈↓ ↓H␈↓Section 6.6). We shall begin by applying these techniques to evaluation.

␈↓ ↓H␈↓As␈αwe␈αsaid␈αearlier␈αthere␈α
are␈αalternatives␈αto␈αthe␈αcall-by-value␈α
evaluation␈αscheme;␈αand␈αas␈αwe␈αall␈α
know
␈↓ ↓H␈↓there are alternatives to the prefix notation which we chose to represent function application.

␈↓ ↓H␈↓For␈αexample,␈αin␈αgrade␈αschool␈αwe␈αall␈α
learned␈αinfix␈αnotation␈αand␈αits␈αimplied␈αprecedence␈αrelations,␈α
say
␈↓ ↓H␈↓for␈α+␈αand␈α*.␈αSimply␈αbecause␈αinfix␈αnotation␈αis␈αthe␈αfirst␈αrepresentation␈αwe␈αsee␈αdoesn't␈αmean␈αthat␈αit␈αis
␈↓ ↓H␈↓the most convenient for evaluation either by us or by machine.

␈↓ ↓H␈↓Let's␈αtake␈αas␈αexample␈αthe␈αexpression:␈α2+3*5.␈α The␈αgrade␈αschool␈αprecedence␈αrelations␈αsay␈αthat␈α*␈α
takes
␈↓ ↓H␈↓precedence␈α∂over␈α∂+.␈α∂ That␈α∂is,␈α∂the␈α∂expression␈α∂represents␈α∂2+(3*5)␈α∂rather␈α∂than␈α∂(2+3)*5.␈α∂ We␈α∂can␈α∂also
␈↓ ↓H␈↓easily␈α
write␈α
the␈α
expression␈α
in␈α
prefix␈αnotation:␈α
+[2;*[3;5]].␈α
 Here␈α
the␈α
precedence␈α
of␈α
operations␈αis␈α
made
␈↓ ↓H␈↓explicit.␈α
Similarly,␈αpostfix␈α
notation␈α
(where␈αthe␈α
operators␈αfollow␈α
rather␈α
than␈αprecede␈α
the␈αoperands)␈α
is
␈↓ ↓H␈↓easy: [2;[3;5]*]+.

␈↓ ↓H␈↓Why␈α↔this␈α↔preoccupation␈α_with␈α↔notation?␈α↔Well,␈α↔some␈α_notational␈α↔schemes␈α↔lend␈α_themselves␈α↔to
␈↓ ↓H␈↓mechanical␈α∪evaluation␈α∪better␈α∪than␈α∪others.␈α∪ There␈α∪is␈α∪a␈α∪certain␈α∪amount␈α∪of␈α∪implied␈α∪intelligence
␈↓ ↓H␈↓required␈α∩in␈α∩the␈α∩usual␈α⊃infix␈α∩scheme.␈α∩ We␈α∩have␈α∩already␈α⊃seen␈α∩one␈α∩very␈α∩mechanical␈α∩method␈α⊃for
␈↓ ↓H␈↓evaluating some prefix expressions: the ␈↓αvalue␈↓ function in Section 3.6.

␈↓ ↓H␈↓Consider␈αthe␈αpostfix␈αnotation.␈αWe␈αwill␈αfirst␈αclaim␈αthat␈αsince␈αwe␈αknow␈αthat␈αplus␈αand␈αtimes␈αare␈αboth
␈↓ ↓H␈↓binary␈αoperations,␈αthe␈αpunctuation,␈α ], [,␈αand␈α; , ␈αis␈αredundant␈α(this␈αis␈αalso␈αtrue␈αfor␈αprefix␈αnotation).
␈↓ ↓H␈↓Thus the string, 2 3 5 * +, contains the same information as [2;[3;5]*]+.

␈↓ ↓H␈↓A␈αstrong␈αpoint␈αof␈αpostfix␈αstring␈α
notation␈αis␈αits␈αease␈αof␈αevaluation.␈α
 Using␈α"↓"␈αto␈αpoint␈αto␈αthe␈α
current
␈↓ ↓H␈↓position␈αin␈αthe␈α
string␈αand␈αusing␈α
the␈α"| ... |"-notation␈αof␈α
page 107␈αto␈αrepresent␈α
the␈αstack,␈αthe␈α
following
␈↓ ↓H␈↓is a trace of the evaluation of the above string, 2 3 5 * +.
␈↓ ↓H␈↓␈↓↓240  Projects␈↓ 26.4␈↓



␈↓ ↓H␈↓↓                       ↓                   ↓
␈↓ ↓H␈↓2 3 5 * + ;|   |  =>  3 5 * + ;| 2 |  => 5 * + ;␈↓ ε_| 3 |  =>
␈↓ ↓H␈↓␈↓ β(␈↓ ε_| 2 |


␈↓ ↓H␈↓↓                ↓
␈↓ ↓H␈↓* + ; | 5 |  =>  + ;␈↓ β(| 15 |  => | 17 |.
␈↓ ↓H␈↓      | 3 |␈↓ β(|  2 |
␈↓ ↓H␈↓      | 2 |


␈↓ ↓H␈↓It␈α∞is␈α∞a␈α∞very␈α∞simple␈α∞task␈α∞to␈α∞program␈α∞this␈α∞scheme␈α∞as␈α∞a␈α∞LISP␈α∞␈↓αprog␈↓.␈α∞ Similarly,␈α∞it␈α∞is␈α∞quite␈α∞simple␈α∞to
␈↓ ↓H␈↓extend this evaluation scheme to n-ary operators.

␈↓ ↓H␈↓Given␈α∞an␈α∂arbitrary␈α∞arithmetic␈α∂expression␈α∞involving␈α∞constants,␈α∂and␈α∞the␈α∂binary␈α∞operations␈α∂of␈α∞plus
␈↓ ↓H␈↓and␈α
times,␈α
we␈αhave␈α
a␈α
straightforward␈α
mechanical␈αevaluation␈α
scheme.␈α
It␈αis␈α
intuitively␈α
clear␈α
how␈αto
␈↓ ↓H␈↓translate␈αinfix␈αexpressions␈αinto␈α
postfix␈αnotation.␈α If␈αwe␈αcould␈α
mechanize␈αthis␈αprocess␈αthen␈αwe␈α
would
␈↓ ↓H␈↓have␈αan␈α
algorithm␈αfor␈αthe␈α
evaluation␈αof␈αinfix␈α
expressions.␈α First␈αlet's␈α
attempt␈αto␈α
describe␈αprecisely
␈↓ ↓H␈↓the␈α∂class␈α∂of␈α∂infix␈α∂expressions␈α∂which␈α∂we␈α∂wish␈α∂to␈α∂evaluate.␈α∂The␈α∂BNF␈α∂notation␈α∂is␈α∂a␈α∂good␈α∞vehicle.
␈↓ ↓H␈↓Perhaps the following:

␈↓ ↓H␈↓␈↓ βx<exp>␈↓ ∧x::= <exp><binop><exp>
␈↓ ↓H␈↓␈↓ βx␈↓ ∧x::= <integer>

␈↓ ↓H␈↓␈↓ βx<binop>␈↓ ∧x::= + | *    .

␈↓ ↓H␈↓There␈αare␈αmany␈αthings␈αwrong␈αwith␈αthis␈αattempt␈αat␈αa␈αgrammar.␈α First,␈αmany␈αexpressions␈αhave␈αmore
␈↓ ↓H␈↓than␈α∞one␈α∂possible␈α∞description␈α∂or␈α∞parse␈α∂tree␈α∞(the␈α∂grammar␈α∞is␈α∂said␈α∞to␈α∂be␈α∞ambiguous).␈α∂Second,␈α∞this
␈↓ ↓H␈↓grammar doesn't express our usual precedence relations. The next attempt is successful:

␈↓ ↓H␈↓␈↓ βx<exp>␈↓ ¬(::= <exp> + <term>␈↓ λH(1)
␈↓ ↓H␈↓␈↓ βx␈↓ ¬(::= <term>␈↓ λH(2)

␈↓ ↓H␈↓␈↓ βx<term>␈↓ ¬(::= <term> * <factor>␈↓ λH(3)
␈↓ ↓H␈↓␈↓ βx␈↓ ¬(::= <factor>␈↓ λH(4)

␈↓ ↓H␈↓␈↓ βx<factor>␈↓ ¬(::= ( <exp> )␈↓ λH(5)
␈↓ ↓H␈↓␈↓ βx␈↓ ¬(::= <integer>␈↓ λH(6)
␈↓ ↓H␈↓␈↓ βx<integer>␈↓ ¬(::= 0 | 1 | 2  ....␈↓ λH(7)


␈↓ ↓H␈↓For example the (only) parsing of 2+3*5 is:
␈↓ ↓H␈↓␈↓↓6.4␈↓ λ#Syntax-directed processes     241␈↓


␈↓ ↓H␈↓                   <exp>
␈↓ ↓H␈↓                     |
␈↓ ↓H␈↓             <exp>  +  <term>
␈↓ ↓H␈↓              |               |
␈↓ ↓H␈↓           <term>     <term>*<factor>
␈↓ ↓H␈↓              |             |           |
␈↓ ↓H␈↓           <factor>  <factor>  <integer>
␈↓ ↓H␈↓               |            |               |
␈↓ ↓H␈↓           <integer>  <integer>      5
␈↓ ↓H␈↓               |           |
␈↓ ↓H␈↓               2          3



␈↓ ↓H␈↓Our next step is based on the following:

␈↓ ↓H␈↓Assumption:␈αGiven␈α
an␈αarbitrary␈α
(well-formed)␈αarithmetic␈α
expression,␈αe,␈α
of␈αour␈α
above␈αclass,␈α
we␈αcan
␈↓ ↓H␈↓␈↓ αhfind␈αthe␈α
left-most␈αwell-formed␈α
subexpression,␈αs,␈α
such␈αthat␈αs␈α
is␈αan␈α
instance␈αof␈α
the␈αRHS
␈↓ ↓H␈↓␈↓ αhof␈αone␈α
of␈αthe␈αrules,␈α
(1)-(7).␈αLet␈α
e␈↓λ'␈↓␈αbe␈αthe␈α
expression␈αobtained␈αfrom␈α
e,␈αby␈α
replacing␈αthe
␈↓ ↓H␈↓␈↓ αhoccurrence of the RHS by the LHS; then our assumption is also applicable to e␈↓λ'␈↓.
␈↓ ↓H␈↓For example,

␈↓ ↓H␈↓␈↓ αλe␈↓ ∧hs␈↓ πλe␈↓λ'␈↓␈↓ 	Xrule

␈↓ ↓H␈↓␈↓ αλ  2+3*5␈↓ ∧h  2␈↓ πλ<integer>+3*5␈↓ 	X(7)
␈↓ ↓H␈↓␈↓ αλ<integer>+3*5␈↓ ∧h<integer>␈↓ πλ<factor>+3*5␈↓ 	X(6)
␈↓ ↓H␈↓␈↓ αλ<factor>+3*5␈↓ ∧h<factor>␈↓ πλ<term>+3*5␈↓ 	X(4)
␈↓ ↓H␈↓␈↓ αλ<term>+3*5␈↓ ∧h<term>␈↓ πλ<exp>+3*5␈↓ 	X(2)
␈↓ ↓H␈↓␈↓ αλ<exp>+3*5␈↓ ∧h3␈↓ πλ<exp>+<integer>*5␈↓ 	X(7)
␈↓ ↓H␈↓␈↓ αλ<exp>+<integer>*5␈↓ ∧h<integer>␈↓ πλ<exp>+<factor>*5␈↓ 	X(6)
␈↓ ↓H␈↓␈↓ αλ<exp>+<factor>*5␈↓ ∧h<factor>␈↓ πλ<exp>+<term>*5␈↓ 	X(4)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>*5␈↓ ∧h5␈↓ πλ<exp>+<term>*<integer>␈↓ 	X(7)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>*<integer>␈↓ ∧h<integer>␈↓ πλ<exp>+<term>*<factor>␈↓ 	X(6)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>*<factor>␈↓ ∧h<term>*<factor>␈↓ πλ<exp>+<term>␈↓ 	X(3)
␈↓ ↓H␈↓␈↓ αλ<exp>+<term>␈↓ ∧h<exp>+<term>␈↓ πλ<exp>␈↓ 	X(1)


␈↓ ↓H␈↓Let␈α
us␈α
now␈αassociate␈α
an␈α
action␈αwith␈α
each␈α
of␈αthe␈α
rules␈α
(1)-(7)␈αsuch␈α
that␈α
whenever␈αwe␈α
apply␈α
one␈αof
␈↓ ↓H␈↓the␈αrules␈αin␈αthe␈αabove␈αreduction␈αtechnique,␈α
we␈αwill␈αalso␈αexecute␈αthe␈αcorresponding␈αaction.␈α
We␈αwill
␈↓ ↓H␈↓also designate an initialization routine which will be executed at the beginning of the reduction.
␈↓ ↓H␈↓␈↓↓242  Projects␈↓ 26.4␈↓



␈↓ ↓H␈↓Initialization:␈αLet␈αV[0:N]␈αbe␈αa␈αvector␈αindexed␈αfrom␈α0␈αto␈αN,␈αwhere␈αN␈αis␈αat␈αleast␈αas␈αlong␈αas␈αthe␈α
input
␈↓ ↓H␈↓character string.  Let i be an integer variable, initialized to 0.

␈↓ ↓H␈↓␈↓ αλ    rule␈↓ β8␈↓ ¬h    action

␈↓ ↓H␈↓␈↓ αλ<exp>␈↓ β8::= <exp> + <term>␈↓ ¬hV(i) ← `+'; i ← i+1
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <term>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<term>␈↓ β8::= <term>*<factor>␈↓ ¬hV(i) ← `*'; i ← i+1
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <factor>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<factor>␈↓ β8::= (<exp>)␈↓ ¬h do nothing
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <integer>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<integer>␈↓ β8::= 0 | 1 | ...␈↓ ¬hV(i) ← 0 | V(i) ← 1 | ... ; i ← i+1


␈↓ ↓H␈↓Again␈αperforming␈αthe␈αreduction␈αof␈αthe␈αexpression,␈α2+3*5,␈αbut␈αnow␈αexecuting␈αthe␈αaction␈αroutines␈αas
␈↓ ↓H␈↓well we find the contents of V will contain the following:

␈↓ ↓H␈↓              V: 0 1 2 3 4 
␈↓ ↓H␈↓                  2
␈↓ ↓H␈↓                  2 3
␈↓ ↓H␈↓                  2 3 5
␈↓ ↓H␈↓                  2 3 5 *
␈↓ ↓H␈↓                  2 3 5 * +


␈↓ ↓H␈↓That is, the postfix form of the arithmetic expression is formed in V.

␈↓ ↓H␈↓So␈α⊂combining␈α⊂the␈α⊂two␈α⊂algorithms,␈α∂(1) infix␈α⊂to␈α⊂postfix␈α⊂translation,␈α⊂and (2) postfix␈α⊂evaluation,␈α∂we
␈↓ ↓H␈↓could␈α
write␈α
an␈α
infix␈α
evaluator.␈α
However,␈α
we␈α
can␈α∞go␈α
one␈α
better.␈α
By␈α
a␈α
simple␈α
change␈α
to␈α∞the␈α
action
␈↓ ↓H␈↓routines we can perform infix evaluation as we reduce (or recognize) the expression.

␈↓ ↓H␈↓Initialization: Let V[0:N] be a vector and let i be an integer-valued variable, initialized to 0.
␈↓ ↓H␈↓␈↓↓6.4␈↓ λ#Syntax-directed processes     243␈↓



␈↓ ↓H␈↓␈↓ αλ  rule␈↓ β8␈↓ ¬h  action

␈↓ ↓H␈↓␈↓ αλ<exp>␈↓ β8::= <exp>+<term>␈↓ ¬hV(i-2) ← V(i-1)+V(i-2); i ← i-1
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <term>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<term>␈↓ β8::= <term>*<factor>␈↓ ¬hV(i-2) ← V(i-1)*V(i-2); i ← i-1
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <factor>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<factor>␈↓ β8::= (<exp>)␈↓ ¬h do nothing
␈↓ ↓H␈↓␈↓ αλ␈↓ β8::= <integer>␈↓ ¬h do nothing

␈↓ ↓H␈↓␈↓ αλ<integer>␈↓ β8::= 0 | 1 | ...␈↓ ¬hV(i) ← 0 | V(i) ← 1| ... ; i ← i+1


␈↓ ↓H␈↓When␈α
the␈α
arithmetic␈α
expression␈α
has␈α
been␈α
recognized,␈α
V(0)␈α
will␈α
contain␈α
the␈α
value␈α
of␈α
that␈α
expression.
␈↓ ↓H␈↓Notice␈αthat␈αthe␈αcombination␈αof␈αV␈αand␈αits␈αindex␈αi,␈αis␈αperforming␈αas␈αa␈αstack␈αin␈αthis␈αtranslator.␈αThat
␈↓ ↓H␈↓is:␈αwhen␈αwe␈αsee␈αan␈αinteger,␈αwe␈αpush␈αit␈α
into␈αthe␈αstack;␈αwhen␈αwe␈αsee␈αa␈α(binary)␈αoperator␈αwe␈α
pop␈αthe
␈↓ ↓H␈↓two operands, perform the operation and push the result back on the stack.

␈↓ ↓H␈↓This␈α⊃technique␈α⊂of␈α⊃associating␈α⊃action␈α⊂routines␈α⊃(also␈α⊂called␈α⊃semantic␈α⊃routines)␈α⊂with␈α⊃the␈α⊃BNF␈α⊂(or
␈↓ ↓H␈↓syntax)␈α∩equations␈α⊃is␈α∩extremely␈α⊃powerful.␈α∩ Such␈α⊃processes␈α∩are␈α⊃called␈α∩syntax-directed.␈α∩ When␈α⊃we
␈↓ ↓H␈↓discuss compilation syntax-directed methods will be used.



␈↓ ↓H␈↓␈↓ ε∨␈↓↓Project␈↓


␈↓ ↓H␈↓Write␈αa␈αLISP␈αprogram␈αto␈αperform␈αinfix␈αto␈αpostfix␈αtranslation;␈αand␈αthen␈αmodify␈αit␈αto␈αperform␈αinfix
␈↓ ↓H␈↓evaluation.␈αWrite␈αyour␈αprograms␈αtwo␈αways:␈αfirst␈αuse␈αan␈αexplicit␈αstack;␈αthen␈αuse␈αrecursion␈αto␈αoperate
␈↓ ↓H␈↓with an implicit stack.



␈↓ ↓H␈↓␈↓ ε∨␈↓↓Project␈↓


␈↓ ↓H␈↓As␈αa␈αfurther␈αexample␈α
of␈αsyntax-directed␈αprocesses␈αrecall␈αthe␈α
set␈αof␈αexpressions␈αevaluated␈αby␈α
␈↓αtgmoaf␈↓:
␈↓ ↓H␈↓the␈α⊃five␈α⊂primitives␈α⊃under␈α⊂composition␈α⊃of␈α⊂functions,␈α⊃all␈α⊂with␈α⊃constant␈α⊂arguments.␈α⊃ Write␈α⊂syntax
␈↓ ↓H␈↓equations and action routines to effect the evaluation of such expressions.
␈↓ ↓H␈↓␈↓↓244  Projects␈↓ 46.5␈↓


␈↓ ↓H␈↓␈↓ ¬.␈↓↓6.5  Syntax-directed I/O␈↓


␈↓ ↓H␈↓It␈α
is␈α
frequently␈αquite␈α
desirable␈α
and␈α
convenient␈αto␈α
enter␈α
input␈α
and␈αreceive␈α
output␈α
in␈αsomething␈α
other
␈↓ ↓H␈↓than␈α∪S-exprs.␈α∪ Recall␈α∪our␈α∀diagram␈α∪on␈α∪page 55.␈α∪ ␈↓αeval␈↓␈α∪demonstrated␈α∀that␈α∪at␈α∪least␈α∪one␈α∀style␈α∪of
␈↓ ↓H␈↓evaluation␈αcan␈αbe␈α
mechanized.␈α What␈αwe␈α
wish␈αto␈αdo␈α
now␈αis␈αexamine␈α
the␈αpossibility␈αof␈α
mechanizing
␈↓ ↓H␈↓the encoding of the input and the decoding of the output.

␈↓ ↓H␈↓Consider␈α∀for␈α∃example,␈α∀the␈α∀problem␈α∃of␈α∀simplification␈α∀of␈α∃algebraic␈α∀expressions.␈α∃ Many␈α∀rather
␈↓ ↓H␈↓sophisticated␈αsimplifiers␈α
have␈αbeen␈α
written␈α([Hea 68],␈α[MAC 74]).␈α
Assume␈αthat␈α
we␈αhave␈αone␈α
named
␈↓ ↓H␈↓␈↓αsimplify␈↓ which expects S-expr input and gives S-expr output. Thus for example:
␈↓ ↓H␈↓α␈↓ α¬(3+4)*x + x =␈↓βI ␈↓α=> (PLUS (TIMES (PLUS 3 4) X) X) =␈↓βsimplify␈↓α=> (TIMES 8 X) =␈↓βO ␈↓α=> 8*x.

␈↓ ↓H␈↓We␈α∞would␈α∂like␈α∞transformations␈α∂I␈α∞and␈α∂O␈α∞done␈α∞automatically.␈α∂M-expr␈α∞notation␈α∂is␈α∞a␈α∂candidate␈α∞for
␈↓ ↓H␈↓such a task.
␈↓ ↓H␈↓α␈↓ αXcons[A;B] =␈↓βI ␈↓α=> (CONS (QUOTE A)(QUOTE B)) =␈↓βeval␈↓α=> (A . B) =␈↓βO ␈↓α=> (A . B)

␈↓ ↓H␈↓Transformation O is the identity in this case.

␈↓ ↓H␈↓In␈α⊂many␈α⊂cases␈α⊃the␈α⊂automatic␈α⊂generation␈α⊃of␈α⊂the␈α⊂I␈α⊃and␈α⊂O␈α⊂transformations␈α⊃can␈α⊂be␈α⊂done.␈α⊃Such␈α⊂a
␈↓ ↓H␈↓program,␈α∞SDIO␈α∞(Syntax Directed Input Output),␈α∞was␈α∞written␈α∞by␈α∞Lynn␈α∞Quam␈α∞of␈α∞the␈α∂Stanford␈α∞AI
␈↓ ↓H␈↓Laboratory␈α≠in␈α≤1968␈α≠[Qua 68].␈α≤It␈α≠was␈α≠the␈α≤forerunner␈α≠of␈α≤the␈α≠more␈α≤ambitious␈α≠MLISP2
␈↓ ↓H␈↓project[Mli 72].␈α
 The␈α
basic␈α
idea␈αis␈α
simple␈α
enough.␈α
We␈α
will␈αassume␈α
that␈α
the␈α
input␈α
and␈αoutput␈α
syntax
␈↓ ↓H␈↓is␈α∞specified␈α∞in␈α∞BNF.␈α∂With␈α∞each␈α∞BNF␈α∞equation␈α∞we␈α∂will␈α∞associate␈α∞semantics␈α∞describing␈α∂the␈α∞S-expr
␈↓ ↓H␈↓representation.␈α→The␈α→input␈α→transformation␈α→(parser)␈α~will␈α→use␈α→this␈α→information␈α→to␈α~build␈α→the
␈↓ ↓H␈↓representation; and the output transformation (unparser) will map the internal representation back.

␈↓ ↓H␈↓The␈α⊂importance␈α⊃of␈α⊂syntax␈α⊃directed␈α⊂I/O␈α⊃should␈α⊂not␈α⊃be␈α⊂minimized.␈α⊃One␈α⊂aspect␈α⊃of␈α⊂SDIO␈α⊃is␈α⊂the
␈↓ ↓H␈↓ablility␈αto␈αdefine␈α
new␈αdata␈αtypes␈α
in␈αLISP.␈αAssume␈αwe␈α
wish␈αto␈αrepresent␈α
a␈αstructure␈αas␈αa␈α
particularly
␈↓ ↓H␈↓horrible␈α≠list␈α≠structure.␈α≠We␈α≠can␈α≠give␈α≠augmented␈α≠BNF␈α≠equations␈α≠specifying␈α≠the␈α~external
␈↓ ↓H␈↓representation␈α
and␈αthe␈α
translation␈αto␈α
the␈αunderlying␈α
representation.␈αClearly␈α
when␈α
outputting␈αthese
␈↓ ↓H␈↓structures␈α∂we␈α∂do␈α∂not␈α⊂want␈α∂to␈α∂see␈α∂the␈α∂internal␈α⊂representation.␈α∂This␈α∂can␈α∂be␈α⊂particularly␈α∂annoying
␈↓ ↓H␈↓when␈αwe␈αare␈αdebugging;␈αwhen␈αwe␈αare␈αtrying␈αto␈αconcentrate␈αon␈αa␈αmisbehaving␈αalgorithm␈αwe␈αdo␈αnot
␈↓ ↓H␈↓want␈αto␈α
be␈αdistracted␈αby␈α
incomprehensible␈αoutput.␈α Thus␈α
syntax␈αdirected␈αoutput,␈α
or␈αunparsing,␈αis␈α
at
␈↓ ↓H␈↓least as important as input.

␈↓ ↓H␈↓Perhaps␈α∩the␈α∪easiest␈α∩introduction␈α∪to␈α∩SDIO␈α∪is␈α∩to␈α∪examine␈α∩an␈α∪example.␈α∩Consider␈α∪the␈α∩proposed
␈↓ ↓H␈↓simplification␈α
task␈α∞above.␈α
The␈α
"natural"␈α∞input␈α
syntax␈α
can␈α∞be␈α
described␈α
in␈α∞BNF.␈α
 We␈α∞have␈α
given
␈↓ ↓H␈↓closely␈αrelated␈αsyntax␈αequations␈αon␈αpage 240.␈αWe␈αwill␈αdisplay␈αa␈αfew␈αequations␈αaugmented␈αby␈αSDIO
␈↓ ↓H␈↓semantics.
␈↓ ↓H␈↓␈↓↓6.5␈↓ λnSyntax-directed I/O     245␈↓



␈↓ ↓H␈↓For example:

␈↓ ↓H␈↓   <EXP>␈↓ βH::= <EXP> + <TERM>␈↓ λ(=>(PLUS EXP TERM)
␈↓ ↓H␈↓␈↓ βH::= <TERM>␈↓ λ(=>*

␈↓ ↓H␈↓   <TERM>␈↓ βH::= <NUMBER>␈↓ λ(=>*


␈↓ ↓H␈↓To␈α∞the␈α∞input␈α∞parser␈α∞the␈α∞first␈α∞BNF␈α∞equation␈α∞means:␈α∞whenever␈α∞the␈α∞right␈α∞hand␈α∞side␈α∞is␈α
recognized,
␈↓ ↓H␈↓reduce␈α∞that␈α∞occurrence␈α∞to␈α∞the␈α∞left␈α∞hand␈α∞side␈α∞and␈α∞associate␈α∞with␈α∞it␈α∞the␈α∞list␈α∞consisting␈α∞of␈α∂the␈α∞atom
␈↓ ↓H␈↓PLUS,␈α∞the␈α∞S-expr␈α∞associated␈α∞with␈α∞the␈α∂occurrence␈α∞of␈α∞<EXP>,␈α∞and␈α∞the␈α∞S-expr␈α∞associated␈α∂with␈α∞the
␈↓ ↓H␈↓occurrrence␈α⊂of␈α⊃<TERM>.␈α⊂ The␈α⊃second␈α⊂equation␈α⊃means␈α⊂reduce␈α⊃<TERM>␈α⊂to␈α⊃<EXP>␈α⊂associating
␈↓ ↓H␈↓whatever␈αS-expr␈αis␈αattached␈αto␈α<TERM>␈αwith␈αthat␈αoccurrence␈αof␈α<EXP>.␈αIn␈αthe␈αthird␈αequation␈α
we
␈↓ ↓H␈↓assume␈αthat␈α<NUMBER>␈αis␈αa␈αsyntactic␈αtype␈αrecognized␈αby␈αthe␈αscanner,␈αand␈αreturn␈αthat␈αnumber␈αas
␈↓ ↓H␈↓the␈α⊂semantic␈α⊂value.␈α⊂ For␈α∂example,␈α⊂if␈α⊂such␈α⊂a␈α∂parser␈α⊂were␈α⊂given␈α⊂2+3+44␈α∂it␈α⊂should␈α⊂return␈α⊂the␈α∂list
␈↓ ↓H␈↓␈↓α(PLUS (PLUS 2 3) 44)␈↓.

␈↓ ↓H␈↓The␈αunparser␈αuses␈α
these␈αequations␈αin␈α
the␈αinverse␈αmanner.␈αIt␈α
will␈αsee␈αa␈α
S-expr␈αand␈αwill␈α
attempt␈αto
␈↓ ↓H␈↓match that to the description of the semantics, outputting an instance of the BNF if successful.

␈↓ ↓H␈↓The␈αSDIO␈αprogram␈αwill␈αtake␈αsuch␈αan␈αaugmented␈αset␈αof␈αBNF␈αequations␈αand␈αgenerate␈αa␈αparser␈αand
␈↓ ↓H␈↓an␈αunparser␈αfor␈αthe␈αlanguage.␈α This␈αproject␈α
involves␈αwriting␈αsuch␈αa␈αSDIO␈αprogram.␈αWe␈αdescribe␈α
a
␈↓ ↓H␈↓basic SDIO program and suggest extensions and improvements.

␈↓ ↓H␈↓The best way to describe the format of SDIO input is to give an SDIO description.
␈↓ ↓H␈↓␈↓↓246  Projects␈↓ 46.5␈↓



␈↓ ↓H␈↓<RULES>␈↓ βH::= END␈↓ λ(=>NIL
␈↓ ↓H␈↓␈↓ βH::=<RULE><RULES>␈↓ λ(=>(RULE . RULES)

␈↓ ↓H␈↓<RULE>␈↓ βH::= <LFPT><RTLST>␈↓ λ(=>(LFPT RTLST)

␈↓ ↓H␈↓<RTLST>␈↓ βH::= ::=<RTPT><SEXPR><RTLST>␈↓ λ(=>((RTPT SEXPR) . RTLST)
␈↓ ↓H␈↓␈↓ βH::= ␈↓ε␈↓␈↓ λ(=>NIL

␈↓ ↓H␈↓<LFPT>␈↓ βH::= <<ID>>␈↓ λ(=>*

␈↓ ↓H␈↓<RTPT>␈↓ βH::= "=>␈↓ λ(=>NIL
␈↓ ↓H␈↓␈↓ βH::= <RPELEM><RTPT>␈↓ λ(=>(RPELEM . RTPT)

␈↓ ↓H␈↓<RPELEM>␈↓ βH::= <<ID>>␈↓ λ(=>*
␈↓ ↓H␈↓␈↓ βH::= <ID>␈↓ λ(=>(SPWD ID)
␈↓ ↓H␈↓␈↓ βH::= ""<CHAR>␈↓ λ(=>(QCH CHAR)
␈↓ ↓H␈↓␈↓ βH::= <CHAR>␈↓ λ(=>(CH CHAR)

␈↓ ↓H␈↓<SEXPR>␈↓ βH::= <ATOM>␈↓ λ(=>*
␈↓ ↓H␈↓␈↓ βH::= (<SEXPRLIST>)␈↓ λ(=>*

␈↓ ↓H␈↓<SEXPRLIST>␈↓ βH::= <ATOM>␈↓ λ(=>*
␈↓ ↓H␈↓␈↓ βH::= <SEXPR> <SEXPRLIST>␈↓ λ(=>(SEXPR . SEXPRLIST)
␈↓ ↓H␈↓␈↓ βH::= ␈↓ε␈↓␈↓ λ(=>NIL

␈↓ ↓H␈↓END

␈↓ ↓H␈↓The␈αexpressions␈α␈↓α(SPWD␈αID),␈α(QCH␈αCHAR),␈↓␈αand␈α␈↓α(CH␈αCHAR)␈↓␈αare␈αS-expr␈αrepresentations␈αof␈αcalls
␈↓ ↓H␈↓on␈α⊗rountines␈α⊗to␈α⊗process␈α⊗special␈α⊗or␈α⊗reserved␈α⊗words,␈α⊗quoted␈α⊗characters␈α⊗or␈α⊗special␈α∃characters,
␈↓ ↓H␈↓respectively.

␈↓ ↓H␈↓The␈αinput␈αto␈αSDIO␈αis␈αa␈αsequence␈αof␈αaugmented␈αBNF␈αequations␈αterminated␈αwith␈αEND.␈α What␈αthe
␈↓ ↓H␈↓SDIO␈α
program␈α
sees␈α
is␈α
a␈α
S-expr␈α
representation␈α
of␈α
these␈α
equations.␈α
The␈α
sample␈α
equations␈α
for␈α
<EXP>
␈↓ ↓H␈↓above would pass the following to the SDIO program:

␈↓ ↓H␈↓␈↓ αX(␈↓ βλ(EXP (␈↓ ∧λ((EXP (CH +) TERM) (PLUS EXP TERM))
␈↓ ↓H␈↓␈↓ αX␈↓ βλ␈↓ ∧λ((TERM) NIL)))
␈↓ ↓H␈↓␈↓ αX␈↓ βλ(TERM (␈↓ ∧λ((NUMBER) NIL))) )

␈↓ ↓H␈↓What the SDIO program outputs are the parser and unparser.

␈↓ ↓H␈↓The␈α∞elements␈α
of␈α∞the␈α∞BNF␈α
equations␈α∞in␈α∞SDIO␈α
are␈α∞rather␈α∞standard:␈α
syntactic␈α∞variables,␈α∞which␈α
are
␈↓ ↓H␈↓identifiers␈α
bracketed␈α
by␈α
"<"␈α
and␈α
">";␈α
and␈α
special␈α
words,␈α
which␈α
are␈α
identifiers;␈α
and␈αspecial␈α
characters,
␈↓ ↓H␈↓which are preceeded by " if they conflict with the special characters of the BNF.
␈↓ ↓H␈↓␈↓↓6.5␈↓ λnSyntax-directed I/O     247␈↓


␈↓ ↓H␈↓The␈α∞elements␈α∂of␈α∞the␈α∞semantics␈α∂are:␈α∞unbracketed␈α∞syntactic␈α∂variables␈α∞occurring␈α∞in␈α∂the␈α∞RHS␈α∂of␈α∞the
␈↓ ↓H␈↓associated␈α∞BNF␈α∞equation;␈α∞other␈α∞identifiers,␈α∞taken␈α∂as␈α∞constants;␈α∞NIL,␈α∞the␈α∞LISP␈α∞atom;␈α∂notation␈α∞for
␈↓ ↓H␈↓␈↓αcons␈↓-ing, ␈↓α(   .   )␈↓; notation for making a list, ␈↓α(e␈↓β1␈↓α ... e␈↓βn␈↓α)␈↓; the character *, described above.



␈↓ ↓H␈↓␈↓ ε∨␈↓↓Project␈↓


␈↓ ↓H␈↓Write␈α∂such␈α∂a␈α∂SDIO␈α∂program.␈α∂You␈α∂should␈α∂consult␈α∂local␈α∂LISP␈α∂documentation␈α∂when␈α∂building␈α∞the
␈↓ ↓H␈↓basic I/O routines like <NUMBER>, <CHAR>, or <ID>.



␈↓ ↓H␈↓␈↓ ¬h␈↓↓First Extension␈↓


␈↓ ↓H␈↓You␈αmay␈αhave␈α
noticed␈αalready␈αthat␈αthe␈α
basic␈αSDIO␈αprogram␈αfails␈α
to␈αdistinguish␈αtwo␈αoccurrences␈α
of
␈↓ ↓H␈↓the same syntactic variable on the RHS of an equation. Thus an equation like:

␈↓ ↓H␈↓<ZIP>␈↓ α8::= <ZAP> <FOO> <ZAP>  must be replaced by the pair:

␈↓ ↓H␈↓<ZIP>␈↓ α8::= <ZAP> <FOO> <ZAP1>
␈↓ ↓H␈↓<ZAP1>␈↓ α8::=<ZAP>

␈↓ ↓H␈↓This trick is called stratification. It is a syntactic trick, adding nothing to the semantics.

␈↓ ↓H␈↓Add␈αnotation␈αto␈αthe␈αsemantics␈αof␈αyour␈αSDIO␈αprogram␈αto␈αhandle␈αRHS␈αwith␈αmultiple␈αoccurrences␈αof
␈↓ ↓H␈↓syntactic variables. Modify your parser generators accordingly.



␈↓ ↓H␈↓␈↓ ¬Y␈↓↓Second Extension␈↓


␈↓ ↓H␈↓Besides␈α
building␈α
a␈α∞S-expr␈α
representation␈α
of␈α∞the␈α
input,␈α
it␈α
is␈α∞frequently␈α
desirable␈α
to␈α∞generate␈α
other
␈↓ ↓H␈↓information␈αduring␈αthe␈αinput␈αparse.␈αLists␈αof␈αoccurrences␈αof␈αoperators␈αor␈αother␈αtables␈αare␈αcommonly
␈↓ ↓H␈↓needed.␈α∂The␈α∞additional␈α∂information␈α∞could␈α∂be␈α∞discovered␈α∂by␈α∞examination␈α∂of␈α∞the␈α∂completed␈α∞parse
␈↓ ↓H␈↓tree,␈α
but␈α
that␈α
requires␈α
reexamination␈α
of␈α
the␈α
tree.␈αIt␈α
is␈α
much␈α
more␈α
efficient␈α
to␈α
do␈α
as␈α
much␈αas␈α
possible
␈↓ ↓H␈↓on a single pass.

␈↓ ↓H␈↓Introduce␈αnotation␈αwhich␈αwill␈αallow␈αexecution␈αof␈αarbitrary␈αLISP␈αcode␈αas␈αthe␈αparse␈αprogresses.␈α
That
␈↓ ↓H␈↓code␈α⊂should␈α⊂be␈α∂able␈α⊂to␈α⊂manipulate␈α⊂any␈α∂of␈α⊂the␈α⊂semantic␈α∂properties␈α⊂associated␈α⊂with␈α⊂the␈α∂syntactic
␈↓ ↓H␈↓variables appearing in the RHS of the associated syntax equation.
␈↓ ↓H␈↓␈↓↓248  Projects␈↓ 46.5␈↓


␈↓ ↓H␈↓␈↓ ¬d␈↓↓Third extension␈↓


␈↓ ↓H␈↓While␈α⊃it␈α⊂is␈α⊃obviously␈α⊂advantageous␈α⊃to␈α⊃produce␈α⊂output␈α⊃in␈α⊂the␈α⊃language␈α⊂described␈α⊃by␈α⊃the␈α⊂BNF
␈↓ ↓H␈↓equations␈α∂rather␈α∂than␈α∞the␈α∂S-expr␈α∂form,␈α∂formatting␈α∞of␈α∂the␈α∂output␈α∞can␈α∂be␈α∂equally␈α∂beneficial.␈α∞ We
␈↓ ↓H␈↓should␈αlike␈αto␈αbe␈αable␈αto␈αspecify␈αformatting␈αinformation␈αin␈αSDIO␈αsuch␈αthat␈αspacing␈αand␈α
line-length
␈↓ ↓H␈↓are controlled.

␈↓ ↓H␈↓One␈α⊂proposal␈α∂is␈α⊂to␈α∂embed␈α⊂spacing␈α∂and␈α⊂line-feed␈α∂control␈α⊂characters␈α∂in␈α⊂the␈α∂BNF␈α⊂equations.␈α∂The
␈↓ ↓H␈↓spacing␈α
character␈α
is␈α
"→"␈α
and␈α
the␈α
line-feed␈α
is␈α
"↓".␈α
 The␈α
"↑"␈α
sets␈α
the␈α
indentation␈α
point␈α
for␈α
the␈α
string␈α
on
␈↓ ↓H␈↓its␈α∂right;␈α∂and␈α∂the␈α∂"→"␈α∂followed␈α∂by␈α∂a␈α∂digit␈α∂says␈α∂space␈α∂over␈α∂than␈α∂number␈α∂of␈α∂spaces␈α∂from␈α⊂the␈α∂last
␈↓ ↓H␈↓indentation␈αpoint␈αif␈αthe␈αremaining␈α
space␈αon␈αthe␈αline␈αis␈αnot␈α
sufficient␈αto␈αcontain␈αall␈αtext␈αspecified␈α
by
␈↓ ↓H␈↓the remaining RHS of the equation.  "0→", meaning go to the indentation point can be written "→".

␈↓ ↓H␈↓For example consider the following:

␈↓ ↓H␈↓␈↓ αX<EXPR>␈↓ ∧8::= <ID>( ↓ <EXPR_LIST>)
␈↓ ↓H␈↓␈↓ αX␈↓ ∧8::= <ID>

␈↓ ↓H␈↓␈↓ αX<EXPR_LIST>␈↓ ∧8::= ↑<EXPR> , →<EXPR_LIST>
␈↓ ↓H␈↓␈↓ αX␈↓ ∧8::= ↑<EXPR>

␈↓ ↓H␈↓These equations, when used to drive an unparser, could give:

␈↓ ↓H␈↓αmumf(␈↓ α(a,
␈↓ ↓H␈↓α␈↓ α(foobaz(garp(b)),
␈↓ ↓H␈↓α␈↓ α(bletch(a,b,c),
␈↓ ↓H␈↓α␈↓ α(d)

␈↓ ↓H␈↓as the formatted version of:
␈↓ ↓H␈↓α␈↓ ∧amumf(a,foobaz(garp(b)),bletch(a,b,c),d)

␈↓ ↓H␈↓Extend SDIO to handle formatting of output.



␈↓ ↓H␈↓␈↓ ∧p␈↓↓6.6  Syntax directed computation␈↓


␈↓ ↓H␈↓compilation for sae

␈↓ ↓H␈↓BNF for mexprs

␈↓ ↓H␈↓syntax directed compiler

␈↓ ↓H␈↓scanner parser
␈↓ ↓H␈↓␈↓↓6.6␈↓ πpSyntax directed computation     249␈↓


␈↓ ↓H␈↓general syntax directed computation
␈↓ ↓H␈↓␈↓↓250  BIBLIOGRPAHY␈↓ C7.␈↓


␈↓ ↓H␈↓␈↓ ¬SBIBLIOGRAPHY␈↓α




␈↓ ↓H␈↓The basic form of an entry consists of three items:

␈↓ ↓H␈↓␈↓ α_␈↓↓1.␈↓ A short name which is how the document is referenced in the text.

␈↓ ↓H␈↓␈↓ α_␈↓↓2.␈↓ The full bibliographical reference.

␈↓ ↓H␈↓␈↓ α_␈↓↓3.␈↓␈α∂A␈α∂sequence␈α⊂of␈α∂pages␈α∂in␈α⊂the␈α∂text␈α∂which␈α∂refer␈α⊂to␈α∂this␈α∂document.␈α⊂If␈α∂the␈α∂document␈α⊂is␈α∂not
␈↓ ↓H␈↓␈↓ α_referenced the statement ␈↓↓[ norefs ]␈↓ appears instead.

␈↓ ↓H␈↓[And 76]␈↓ αxAnderson, D., A brief critique of LISP, AISB Conference, Essex, 1976 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bar 66]␈↓ αxBarron,␈αD.␈αand␈αStrachey,␈αC.,␈αProgramming,␈αAdvances␈αin␈αcomputer␈αprogramming␈αand
␈↓ ↓H␈↓␈↓ βλnon-numerical computation (ed. L. Fox),49-82 ␈↓↓[␈↓␈↓↓ 146␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bar 71]␈↓ αxBarbacci,␈α⊃M,␈α⊃␈↓αet␈α⊃al␈↓,␈α⊃C.ai (P.LISP),␈α⊃A␈α⊃LISP␈α⊃processor␈α⊃of␈α⊃C.ai,␈α∩Carnegie-Mellon␈α⊃U.,
␈↓ ↓H␈↓␈↓ βλ1971 ␈↓↓[␈↓␈↓↓ 178␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ber 64]␈↓ αxBerkekey,␈α∞E.,␈α∞and␈α∞Bobrow,␈α∞D.,␈α∞The␈α∞programming␈α∞language␈α∞LISP: Its␈α∂operation␈α∞and
␈↓ ↓H␈↓␈↓ βλapplications, Information International, Cambridge, Mass., 1964 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ber 70]␈↓ αxBerry,␈α+D.,Block␈α+structure: retention␈α+or␈α+deletion?,␈α+Tr 70-29,␈α,Center␈α+for
␈↓ ↓H␈↓␈↓ βλcomputer & Information Science, Brown University, Dec 1970 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ber 71]␈↓ αxBerkling,␈αH.,␈αA␈αcomputing␈αmachine␈αbased␈αon␈αtree␈αstructures,␈α␈↓αIEEE␈αTrans␈αon␈αComptr.
␈↓ ↓H␈↓α␈↓ βλ20, C-20␈↓, ␈↓↓4␈↓, (Apr. 71) ␈↓↓[␈↓␈↓↓ 184␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bis 74a]␈↓ αxBishop,␈α⊂P.,␈α⊃Garbage␈α⊂collection␈α⊃in␈α⊂a␈α⊃very␈α⊂large␈α⊃address␈α⊂space,␈α⊃Working␈α⊂paper 111,
␈↓ ↓H␈↓␈↓ βλM.I.T. A.I. Lab, Sep. 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bis 74]␈↓ αxBishop, P., ␈↓αSpaghetti Stacks␈↓, unpublished paper, M.I.T., Dec 19, 1974 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bla 71]␈↓ αxBlair, F., ␈↓αThe Structure of the Lisp Compiler␈↓, unpublished paper, 1971 ␈↓↓[␈↓␈↓↓ 167␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bob 67]␈↓ αxBobrow,␈αD.␈αand␈αMurphy,␈αD.,␈αThe␈αstructure␈αof␈αa␈αLISP␈αsystem␈αusing␈αtwo-level␈αstorage,
␈↓ ↓H␈↓␈↓ βλ␈↓αComm. ACM 10␈↓, ␈↓↓3␈↓, (Mar. 1967), xx-yy ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bob 73a]␈↓ αxBobrow,␈α∃D.␈α∃and␈α∃Wegbreit,␈α∃B.,␈α∃A␈α∃model␈α∃and␈α∃stack␈α∃implementation␈α⊗of␈α∃multiple
␈↓ ↓H␈↓␈↓ βλenvironments, ␈↓αComm. ACM 16␈↓, ␈↓↓10␈↓, (Oct. 1973), 591-603 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Bob 73]␈↓ αxBobrow,␈α∃D.␈α∃and␈α∃Raphael,␈α∃D.,␈α∃New␈α∃Programming␈α∃Languages␈α∃for␈α∃A.I.␈α∃Research
␈↓ ↓H␈↓␈↓ βλStanford Research Institute, Menlo Park, Ca., 1973 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓7.␈↓ 	
BIBLIOGRPAHY     251␈↓


␈↓ ↓H␈↓[Bob 75]␈↓ αxBobrow, D., A note on hash linking, ␈↓αComm. ACM 18␈↓, ␈↓↓7␈↓, (Jul. 1975), 413-414 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Boy 75]␈↓ αxBoyer,␈α⊃R.␈α⊃and␈α⊂Moore,␈α⊃J,␈α⊃Proving␈α⊃theorems␈α⊂about␈α⊃LISP␈α⊃functions,␈α⊃␈↓αJour. ACM ␈↓,␈α⊂␈↓↓1␈↓,
␈↓ ↓H␈↓␈↓ βλ(Mar. 1975), 129-144 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Car 76]␈↓ αxCartwright,␈αR.,␈αA␈α
practical␈αand␈αFormal␈αsemantics␈α
and␈αverification␈αsystem␈αfor␈α
TYPED
␈↓ ↓H␈↓␈↓ βλLISP, Ph.D. Thesis, Computer Science Dept., Stanford Univ., 1976 ␈↓↓[␈↓␈↓↓ 179␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Che 67]␈↓ αxCheatham,␈α∀T.,␈α∀The␈α∀Theory␈α∃&␈α∀Construction␈α∀of␈α∀Compilers,␈α∃Computer␈α∀Associates,
␈↓ ↓H␈↓␈↓ βλWakefield, Mass., 1967 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Che 70]␈↓ αxCheney,␈α~C.,␈α~A␈α~Nonrecursive␈α~List␈α~Compacting␈α~Algorithm,␈α~␈↓αComm. ACM 13␈↓,␈α→␈↓↓11␈↓,
␈↓ ↓H␈↓␈↓ βλ(Nov. 1970), 677-678 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Chu 41]␈↓ αxChurch,␈α∃A.,␈α∃The␈α∃calculi␈α∃of␈α∀lambda␈α∃conversion,␈α∃Annals␈α∃of␈α∃mathematics␈α∀studies,
␈↓ ↓H␈↓␈↓ βλPrinceton University press, 1941 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Con 73]␈↓ αxMcDermott,␈α⊗D.␈α∃and␈α⊗Sussman,␈α∃G.,␈α⊗The␈α∃CONNIVER␈α⊗Reference␈α⊗Manual,␈α∃M.I.T.
␈↓ ↓H␈↓␈↓ βλA.I. Lab. Memo 259a, Cambridge, Mass 1973 ␈↓↓[␈↓␈↓↓ 167␈↓␈↓↓ 236␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Con 74]␈↓ αxConrad,␈α⊂W.,␈α⊃A␈α⊂compactifying␈α⊂garbage␈α⊃collector␈α⊂for␈α⊂ECL's␈α⊃non-homogeneous␈α⊂heap,
␈↓ ↓H␈↓␈↓ βλTR. 2-74,␈α*Center␈α+for␈α*Research␈α+in␈α*Computing␈α+Technology,␈α*Harvard,
␈↓ ↓H␈↓␈↓ βλFeb. 1974 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Dar 73]␈↓ αxDarlington,␈α⊂J.␈α⊂and␈α⊃Burstall,␈α⊂R.,␈α⊂A␈α⊃system␈α⊂which␈α⊂automatically␈α⊃improves␈α⊂programs,
␈↓ ↓H␈↓␈↓ βλProc. 3␈↓πth␈↓ Int. J. Conf. on A.I., Stanford, 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Deu 73]␈↓ αx178 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Dif 71]␈↓ αxDiffie,␈α∂W.,␈α∂Documentation␈α⊂of␈α∂the␈α∂compiler,␈α⊂unpublished␈α∂paper,␈α∂Stanford␈α⊂A.I.␈α∂Lab.,
␈↓ ↓H␈↓␈↓ βλ1971 ␈↓↓[␈↓␈↓↓ 200␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[DSIPL]␈↓ αxProceedings␈α
of␈αa␈α
Symposium␈αon␈α
Data␈α
Structures␈αin␈α
Programming␈αLanguages,␈α
␈↓αSigplan
␈↓ ↓H␈↓α␈↓ βλNotices 6␈↓, ␈↓↓2␈↓, (Feb. 1971) ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[EL1 74]␈↓ αxWegbreit,␈α⊗Ben.,␈α⊗ECL␈α∃programmer's␈α⊗manual,␈α⊗TR 23-74,␈α∃Center␈α⊗for␈α⊗Research␈α∃in
␈↓ ↓H␈↓␈↓ βλComputing Technology, Harvard Univ., Cambridge, Dec. 1974 ␈↓↓[␈↓␈↓↓ 200␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Fel 68]␈↓ αxFeldman,␈αJ.␈αand␈αGreis,␈αD.,␈αTranslator␈αWriting␈αSystems,␈α␈↓αComm. ACM␈α11␈↓,␈α␈↓↓2␈↓,␈α
(Feb. 1968),
␈↓ ↓H␈↓␈↓ βλ77-113 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Fis 70]␈↓ αxFisher,␈α∩D.,␈α∩Control␈α∩structures␈α∪for␈α∩programming␈α∩languages,␈α∩Ph.D. Thesis,␈α∪Dept.␈α∩of
␈↓ ↓H␈↓␈↓ βλComputer Science, Carnegie-Mellon University, 1790 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Fis 72]␈↓ αxFischer,␈α⊂M.,␈α⊂Lambda␈α⊂Calculus␈α⊂schemata,␈α⊂ACM␈α⊂Conf.␈α⊂on␈α⊂Proving␈α⊂assertions␈α⊂about
␈↓ ↓H␈↓␈↓ βλprograms, SIGPLAN Notices, Jan. 1972 ␈↓↓[␈↓␈↓↓ 155␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓252  BIBLIOGRAPHY␈↓ C7.␈↓


␈↓ ↓H␈↓[Fri 75A␈↓ αxFriedman,␈αD.␈αand␈αWise,␈αD.,␈α
CONS␈αShould␈αNot␈αEvaluate␈αits␈α
Arguments,␈αTR. No. 44,
␈↓ ↓H␈↓␈↓ βλComputer Science Dept., Indiana University, Nov. 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Fri 75a]␈↓ αx169 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Fri 75]␈↓ αxFriedman,␈α⊗D.␈α⊗and␈α⊗Wise,␈α∃D.,␈α⊗Multiple-valued␈α⊗Recursive␈α⊗Procedures,␈α∃TR. No. 27,
␈↓ ↓H␈↓␈↓ βλComputer Science Dept., Indiana University, Apr. 1975 ␈↓↓[␈↓␈↓↓ 170␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Gol 73]␈↓ αxGoldstein,␈α∃I.,␈α∃Pretty␈α∃printing: converting␈α∃list␈α∃to␈α∃linear␈α⊗structure,␈α∃.M.I.T. A.I. Lab,
␈↓ ↓H␈↓␈↓ βλMemo 279, Feb. 1973 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Gor 73]␈↓ αxGordon,␈α
M.,␈α
Models␈α
of␈α
pure␈α
LISP,␈α
Experimental␈α
programming␈α∞reports: No.30,␈α
Dept.
␈↓ ↓H␈↓␈↓ βλof Machine Intelligence, University of Eningurgh, 1973 ␈↓↓[␈↓␈↓↓ 177␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Gor 75]␈↓ αxGordon,␈α∩M.,␈α⊃Towards␈α∩a␈α∩semanitc␈α⊃theory␈α∩of␈α∩dynamic␈α⊃binding,␈α∩Stanford␈α∩A.I.␈α⊃Lab.
␈↓ ↓H␈↓␈↓ βλMemo 265, Stanford University, 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Got 74]␈↓ αxGoto,␈α
E.,␈α
Monocopy␈α
and␈α
Associative␈αAlgorithms␈α
in␈α
an␈α
Extended␈α
Lisp,␈α
University␈αof
␈↓ ↓H␈↓␈↓ βλTokyo, Japan, May 1974 ␈↓↓[␈↓␈↓↓ 221␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Got 76]␈↓ αxGoto,␈α⊂E.␈α∂and␈α⊂Kanada,␈α⊂Y.,␈α∂Recursive␈α⊂Hashed␈α⊂Data␈α∂Structures␈α⊂with␈α⊂Applications␈α∂to
␈↓ ↓H␈↓␈↓ βλPolynomial Manipulations, to be submitted to SYMSAC 76 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Got xx]␈↓ αx178 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Gre 74]␈↓ αxGreenblatt, R., The LISP machine, Working paper No. 79, M.I.T., Nov. 1974 ␈↓↓[␈↓␈↓↓ 178␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Gre 75]␈↓ αxGreussay,␈α→P.,␈α→Manual␈α→de␈α→Reference␈α→Provisoire: LISP T 1600,␈α→Universite␈α→Paris,
␈↓ ↓H␈↓␈↓ βλFeb. 1975 ␈↓↓[␈↓␈↓↓ 167␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Gri 71]␈↓ αxGries, D., Compiler Construction for digital computers, Wiley, 1971 ␈↓↓[␈↓␈↓↓ 213␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Gua xx]␈↓ αxGuard,␈α∩J.,␈α∪Semi␈α∩automated␈α∪mathematics,␈α∩␈↓αJrnl. ACM WRONG!!16␈↓,␈α∪␈↓↓10␈↓,␈α∩(Oct. 1973),
␈↓ ↓H␈↓␈↓ βλ591-603 ␈↓↓[␈↓␈↓↓ 184␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ham 68]␈↓ αxHammer, M., Formal Definition of BASEL  ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Han 69]␈↓ αxHansen,␈α∪W.,␈α∪The␈α∪Impact␈α∪of␈α∪Storage␈α∪Management␈α∪on␈α∪Plex␈α∪Processing␈α∪Language
␈↓ ↓H␈↓␈↓ βλImplementation, Stanford Graphics Project, July 1969 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Har 64]␈↓ αxHart,␈α⊂T.,␈α∂and␈α⊂Evans,␈α∂T.,␈α⊂Notes␈α∂on␈α⊂implementing␈α∂LISP␈α⊂for␈α∂th␈α⊂M-460␈α⊂computer,␈α∂in
␈↓ ↓H␈↓␈↓ βλ[Ber 64], 191-203 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Har 75]␈↓ αxHoward, F.,Documentation of Harvard lisp, xx ␈↓↓[␈↓␈↓↓ 182␈↓␈↓↓ 213␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓7.␈↓ 	
BIBLIOGRPAHY     253␈↓


␈↓ ↓H␈↓[Hea 68]␈↓ αxHearn,␈α~A.,␈α→REDUCE␈α~User's␈α~Manual,␈α→Stanford␈α~AIM-50,␈α~Stanford␈α→University,
␈↓ ↓H␈↓␈↓ βλ1968 ␈↓↓[␈↓␈↓↓ 244␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hen 75]␈↓ αxvon␈αHenke,␈αF.,␈α
On␈αthe␈αrepresentation␈α
of␈αdata␈αstructures␈α
in␈αLCF␈αwith␈α
applications␈αto
␈↓ ↓H␈↓␈↓ βλprogram generation, Stanford A.I. Lab., Memo AIM 267, Sep. 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hen 76]␈↓ αxHenderson,␈αP.␈αand␈αMorris,␈αJ.,␈αA␈αLazy␈αevaluator,␈αSIGPLAN-SIGACT␈αSymposium␈αon
␈↓ ↓H␈↓␈↓ βλprinciples of programming languages, Altanta, Jan. 1976, 95-103 ␈↓↓[␈↓␈↓↓ 169␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hew 72]␈↓ αxHewitt,␈α⊂C.,␈α⊂Description␈α⊂and␈α⊃Theoretical␈α⊂Analysis␈α⊂(using␈α⊂Schemata)␈α⊃of␈α⊂PLANNER,
␈↓ ↓H␈↓␈↓ βλTR-258, M.I.T. A.I. Lab., April 1972 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hew 75]␈↓ αxHewitt,␈α∩C.␈α∪and␈α∩Smith,␈α∪B.,␈α∩Towards␈α∪a␈α∩programming␈α∪apprentice,␈α∩IEEE␈α∪Trans.␈α∩on
␈↓ ↓H␈↓␈↓ βλSoftware Engineering, SE-1, (Mar 1975), 26-45 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hew 76]␈↓ αxHewitt,␈α∩C.,␈α∩Viewing␈α∩Control␈α∩structures␈α∩as␈α∩patterns␈α∩of␈α∩passing␈α∩messages,␈α∩Working
␈↓ ↓H␈↓␈↓ βλpaper 92, M.I.T. A.I. Lab, April 1976 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hoa 73a]␈↓ αxHoare,␈α→C.A.R.,␈α~Hints␈α→on␈α→Programming␈α~Language␈α→Design,␈α~Stanford␈α→AIM-224,
␈↓ ↓H␈↓␈↓ βλ(Dec. 1973) ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hoa 73]␈↓ αxHoare, C.A.R., Recursive data structures, Stanford AIM-223, (Oct. 1973) ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Hop xx]␈↓ αx28 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Int 75]␈↓ αxTietelman,␈α⊃W.,␈α⊃INTERLISP␈α⊂reference␈α⊃manual,␈α⊃Xerox␈α⊂Palo␈α⊃Alto␈α⊃Research␈α⊂Center,
␈↓ ↓H␈↓␈↓ βλ1975 ␈↓↓[␈↓␈↓↓ 143␈↓␈↓↓ 179␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Joh 71]␈↓ αxJohnston,␈α↔J.,␈α⊗The␈α↔Contour␈α↔Model␈α⊗of␈α↔Block␈α⊗Structured␈α↔Processes,␈α↔in␈α⊗[DISPL],
␈↓ ↓H␈↓␈↓ βλ55-82 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Kan 75]␈↓ αxKanada,␈α~Y.,␈α→Implementation␈α~of␈α→HLISP␈α~and␈α→algebraic␈α~manipulation␈α→language
␈↓ ↓H␈↓␈↓ βλREDUCE-2,␈α∀Tr 75-01,␈α∀Information␈α∀Sciences␈α∀Lab.␈α∀Univ.␈α∀of␈α∀Tokyo,␈α∀Japan,␈α∀Jan.
␈↓ ↓H␈↓␈↓ βλ1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Knu 68]␈↓ αxKnuth,␈α⊂D.,␈α⊂The␈α⊂Art␈α⊂of␈α⊂Computer␈α⊂Programming,␈α⊂non-numerical␈α⊂algorithms,␈α∂Vol. 1,
␈↓ ↓H␈↓␈↓ βλAddison-Wesley, 1968 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Knu 74]␈↓ αxKnuth,␈αD.,␈αStructured␈αProgramming␈αwith␈αGO␈αTO␈αstatements,␈α␈↓αComputer␈αSurveys␈α6␈↓,␈α4,
␈↓ ↓H␈↓␈↓ βλ(Dec. 1974), 261-301 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Knu xx]␈↓ αxKnuth,␈α∃D.,␈α∀The␈α∃Art␈α∃of␈α∀Computer␈α∃Programming,␈α∀searching␈α∃and␈α∃sorting,␈α∀Vol. y,
␈↓ ↓H␈↓␈↓ βλAddison-Wesley, 1968 ␈↓↓[␈↓␈↓↓ 213␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Lan 64]␈↓ αxLandin,␈α↔P.,␈α↔The␈α_mechanical␈α↔evaluation␈α↔of␈α↔expressions,␈α_␈↓αComputer Journal 6␈↓,␈α↔␈↓↓4␈↓,
␈↓ ↓H␈↓␈↓ βλ(Apr. 1964) ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓254  BIBLIOGRAPHY␈↓ C7.␈↓


␈↓ ↓H␈↓[Lis 74]␈↓ αxLiskov,␈α
B.␈α
and␈α
Zilles,␈α
S.,␈α∞Programming␈α
with␈α
abstract␈α
data␈α
structures,␈α
Proc.␈α∞of␈α
Symp.
␈↓ ↓H␈↓␈↓ βλon Very high level languages, SIGPLAN Notices, Apr. 1974 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Lon 71]␈↓ αxLondon,␈α⊂R.,␈α⊂Correctness␈α⊂of␈α⊂Two␈α⊂Compilers␈α⊂for␈α⊂a␈α⊂LISP␈α⊂Subset,␈α⊃Stanford␈α⊂A.I. Lab.
␈↓ ↓H␈↓␈↓ βλMemo, AIM-151, Oct. ,1971 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Lug 73]␈↓ αxLugger,␈α!J.␈α"and␈α!Melenk,␈α!H.,␈α"Darstellung␈α!und␈α"Bearbeitung␈α!umfangreicher
␈↓ ↓H␈↓␈↓ βλLISP-programme, ␈↓αAngewandte Informatik␈↓, (Jun 1973), 257-263) ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[MAC 74]␈↓ αxBogen,␈α⊃R.,␈α⊃MACSYMA␈α⊃reference␈α⊂manual,␈α⊃Project␈α⊃MAC,␈α⊃Mathlab␈α⊃Group,␈α⊂M.I.T.,
␈↓ ↓H␈↓␈↓ βλCambridge, 1974 ␈↓↓[␈↓␈↓↓ 244␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Man 74]␈↓ αxManna, Z., Theory of Computation, McGraw-Hill, New York, 1974 ␈↓↓[␈↓␈↓↓ 177␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[McC 60]␈↓ αxMcCarthy,␈αJ,␈α
Recursive␈αFunctions␈αof␈α
Symbolic␈αExpressions␈α
and␈αtheir␈αComputation␈α
by
␈↓ ↓H␈↓␈↓ βλMachine, ␈↓αComm. ACM␈↓, (April 1960), 184-195 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[McC 61a]␈↓ αxMcCarthy, J., Micro algol  ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[McC 63]␈↓ αxMcCarthy,␈α⊃J.,␈α⊃A␈α⊂Basis␈α⊃for␈α⊃a␈α⊃Mathematical␈α⊂Theory␈α⊃of␈α⊃Computation,␈α⊃in␈α⊂Computer
␈↓ ↓H␈↓␈↓ βλProgramming and Formal Systems, North Holland, Amsterdam, 1963 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[McC 65]␈↓ αxMcCarthy,␈α⊗J,␈α⊗␈↓αet.␈α⊗al␈↓,␈α⊗LISP␈α∃1.5␈α⊗programmer's␈α⊗manual,␈α⊗M.I.T.␈α⊗Press,␈α∃Cambridge,
␈↓ ↓H␈↓␈↓ βλ1965 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[McD 75]␈↓ αxMcDermott,␈α∩D.,␈α∪Very␈α∩large␈α∩PLANNER-type␈α∪data␈α∩bases,␈α∪A.I. Memo 339,␈α∩A.I. Lab,
␈↓ ↓H␈↓␈↓ βλM.I.T., Cambridge, Mass, Sep. 1975 ␈↓↓[␈↓␈↓↓ 192␈↓␈↓↓ 193␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Men 64]␈↓ αxMendelson,␈α_E.,␈α_Introduction␈α_to␈α_Mathematical␈α_Logic,␈α_Van␈α_Nostrand,␈α_Princeton,
␈↓ ↓H␈↓␈↓ βλNew Jersey, 1964 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mic 71]␈↓ αxSussman,␈α∪G.,␈α∪␈↓αet␈α∪al␈↓,␈α∪Micro-PLANNER␈α∪Reference␈α∪Manual.␈α∪AI Memo 203a.␈α∪M.I.T.,
␈↓ ↓H␈↓␈↓ βλA.I. Lab., Cambridge, Mass., (Dec 1971) ␈↓↓[␈↓␈↓↓ 236␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mit 70]␈↓ αxMitchell,␈α
J.,␈α
The␈α
design␈α
&␈αconstruction␈α
of␈α
flexible␈α
&␈α
efficient␈αinteractive␈α
programming
␈↓ ↓H␈↓␈↓ βλsystems, Ph.D. thesis,Carnegie-Mellon Unversity, Jun. 1970 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mli 72]␈↓ αx244 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mon 73]␈↓ αxMontangero,␈α∃C.,␈α∃␈↓αet␈α∃al␈↓,␈α∃An␈α∃extended␈α∃LISP␈α∃system␈α∃for␈α⊗complex␈α∃control-structure
␈↓ ↓H␈↓␈↓ βλprogramming, University of Pisa, 1973 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mon 75a]␈↓ αxMontangero,␈αC.,␈α␈↓αet␈αal␈↓,␈αMAGMA-Lisp:␈αA␈α`machine␈αlanguage'␈αfor␈αArtifical␈αIntelligence,
␈↓ ↓H␈↓␈↓ βλProc. 4␈↓πth␈↓ Int. J. Conf. on A.I., Tbilisi, (Sep. 1975), 556-561 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓7.␈↓ 	
BIBLIOGRPAHY     255␈↓


␈↓ ↓H␈↓[Mon 75]␈↓ αxMontangero,␈αC.␈α␈↓αet␈αal␈↓,␈αInformation␈αManagement␈αin␈αContext␈αTrees,␈αUniversity␈αof␈αPisa,
␈↓ ↓H␈↓␈↓ βλN.I. B75-21, Oct. 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Moo 76]␈↓ αxMoon,␈α⊂D.,␈α⊂␈↓αMacLISP␈α⊂Reference␈α⊃Manual␈↓,␈α⊂Laboratory␈α⊂for␈α⊂Computer␈α⊃Science,␈α⊂M.I.T.,
␈↓ ↓H␈↓␈↓ βλCambridge, Mass, 1976 ␈↓↓[␈↓␈↓↓ 143␈↓␈↓↓ 144␈↓␈↓↓ 148␈↓␈↓↓ 215␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Moor 72]␈↓ αxMoore, J, Thesis of proving properties  ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Moor 75]␈↓ αxMoore,␈α⊃J,␈α⊃The␈α⊂INTERLISP␈α⊃virtual␈α⊃machine␈α⊂specification␈α⊃(in␈α⊃preparation),␈α⊂Xerox
␈↓ ↓H␈↓␈↓ βλPalo Alto Research Center Report, 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mor xx]␈↓ αxMorris,␈α"L.,␈α#Advice␈α"of␈α"structuring␈α#compilers␈α"and␈α"proving␈α#them␈α"correct,
␈↓ ↓H␈↓␈↓ βλ144-152 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mor yy]␈↓ αxMorris, J., Towards more flexible type systems, 377-383 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mos 70]␈↓ αxMoses,␈α⊃Joel,␈α∩The␈α⊃function␈α∩of␈α⊃FUNCTION␈α∩in␈α⊃LISP,␈α∩Sigsam␈α⊃Bulletin,␈α∩JUly␈α⊃1970,
␈↓ ↓H␈↓␈↓ βλ13-27 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mot 76]␈↓ αxMotoyoshi,␈αF.,␈αA␈α
portable␈αLISP␈αCompiler␈αon␈α
a␈αHypothetical␈αLISP␈αMachine,␈α
TR 76-5,
␈↓ ↓H␈↓␈↓ βλDept. of info. science, University of Tokyo, Japan, (Jan. 1976) ␈↓↓[␈↓␈↓↓ 185␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Mud 75]␈↓ αxGalley,␈α↔S.W.␈α↔and␈α↔Pfister,␈α_G.,␈α↔The␈α↔MDL␈α↔Language.␈α_Programming␈α↔Technology
␈↓ ↓H␈↓␈↓ βλDivision Doc. SYS.11.01. Project Mac, M.I.T., Cambridge, Mass, (Nov. 1975) ␈↓↓[␈↓␈↓↓ 236␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Nor 70]␈↓ αxNordstrom,␈α∞M.,␈α∞␈↓αet␈α∂al␈↓,␈α∞LISP␈α∞F1␈α∂-␈α∞A␈α∞Fortran␈α∂implementation␈α∞of␈α∞LISP␈α∂1.5,␈α∞Computer
␈↓ ↓H␈↓␈↓ βλScience Dept, Uppsala University, Sweden, 1970 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Org 71]␈↓ αxOrganick,␈αE.␈αand␈αCleary,␈αJ.,␈αA␈αData␈αStructure␈αModel␈αof␈αthe␈αB6700␈αComputer␈αSystem,
␈↓ ↓H␈↓␈↓ βλin [DISPL], 83-145 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Pac 73]␈↓ αxPacini,␈α↔G.,␈α↔An␈α↔Optimal␈α↔Fix-point␈α↔Computation␈α↔Rule␈α↔for␈α↔a␈α_Simple␈α↔Recursive
␈↓ ↓H␈↓␈↓ βλLanguage, University of Pisa, N.I. B75-10, Oct. 1973 ␈↓↓[␈↓␈↓↓ 169␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Pag 76]␈↓ αxPage, R., LISP for Fairchild F8, Private communication, 1976 ␈↓↓[␈↓␈↓↓ 182␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Per 67]␈↓ αxPerlis,␈α→A.,␈α→The␈α→synthesis␈α→of␈α→algorithmic␈α→systems,␈α→␈↓αJour. ACM x␈↓,␈α→␈↓↓1␈↓,␈α_(Jan. 1967),
␈↓ ↓H␈↓␈↓ βλxxx-xxx ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Pop 68]␈↓ αxBurstall, R., ␈↓αet al␈↓, POP2 papers, Oliver & Boyd, Eninburgh, 1968 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Qua 68]␈↓ αxQuam, L., SDIO manual, unpublished paper, Stanford, 1968 ␈↓↓[␈↓␈↓↓ 244␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Qua xx]␈↓ αxQuam,␈αL.,&␈αDiffie,␈αW.,␈αStanford␈αLISP 1.6␈αmanual,␈αSAILON 28.7,␈αStanford␈αA.I. Lab.,
␈↓ ↓H␈↓␈↓ βλStanford ␈↓↓[␈↓␈↓↓ 143␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓256  BIBLIOGRAPHY␈↓ C7.␈↓


␈↓ ↓H␈↓[Rey 68]␈↓ αxReynolds,␈α∞J.,␈α∞Definitional␈α∞interpreters␈α
for␈α∞high-order␈α∞programming␈α∞languages,␈α
ACM
␈↓ ↓H␈↓␈↓ βλNational convention, 1972 ␈↓↓[␈↓␈↓↓ 167␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ric 74]␈↓ αxRich,␈α∂C.,␈α∂and␈α∂Shrobe,␈α∞H.,␈α∂Understanding␈α∂LISP␈α∂programs: Towards␈α∂a␈α∞programming
␈↓ ↓H␈↓␈↓ βλapprentice,Working paper 82, M.I.T. A.I. Lab, Dec. 1974 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Rog 67]␈↓ αxRogers,␈αH.,␈α
Theory␈αof␈α
Recursive␈αFunctions␈α
&␈αEffective␈α
Computability,␈αMcGraw-Hill,
␈↓ ↓H␈↓␈↓ βλNew York, 1967 ␈↓↓[␈↓␈↓↓ 25␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Sam 75]␈↓ αxSamet,␈α≠H.,␈α≠Automatically␈α≠Proving␈α~the␈α≠Correctness␈α≠of␈α≠Translations␈α~Involving
␈↓ ↓H␈↓␈↓ βλOptimized Code, Stanford A.I. Lab. Memo, AIM-259, May 1975 ␈↓↓[␈↓␈↓↓ 197␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[San 75a]␈↓ αxSandewall,␈α∞E.,␈α∞Ideas␈α∞about␈α∞management␈α∂of␈α∞LISP␈α∞data␈α∞bases,␈α∞Memo 332,␈α∂M.I.T.␈α∞A.I.
␈↓ ↓H␈↓␈↓ βλLab., May 1975 ␈↓↓[␈↓␈↓↓ 200␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[San 75]␈↓ αxSandewall,␈α⊂E.,␈α⊂Some␈α⊂Observations␈α⊂on␈α⊂Conceptual␈α⊂Programming,␈α⊂Computer␈α∂Science
␈↓ ↓H␈↓␈↓ βλDept., Uppsala University, Sweden, 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[San 76]␈↓ αxSandewall,␈α∂E.,␈α⊂Programming␈α∂in␈α∂an␈α⊂Interactive␈α∂Environment:␈α∂The␈α⊂LISP␈α∂experience,
␈↓ ↓H␈↓␈↓ βλLinkoping University, 1976 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Sco 72]␈↓ αxScott, D., Princeton paper on semantics  ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ste 76a]␈↓ αxSteel,␈α≠G.␈α≠Multiprocessing␈α≠compactifying␈α≠garbage␈α≠collection,␈α≤␈↓αComm. ACM 18␈↓,␈α≠␈↓↓9␈↓,
␈↓ ↓H␈↓␈↓ βλ(Sep. 1967), 495-508 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ste 76b]␈↓ αxSteel,␈αG.␈αand␈αSussman,␈αG.,LAMBDA: the␈αultimate␈α
imperative,␈αM.I.T. A.I. Memo 353,
␈↓ ↓H␈↓␈↓ βλMar. 1976 ␈↓↓[␈↓␈↓↓ 167␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ste pc]␈↓ αxSteel, G., private communications  ␈↓↓[␈↓␈↓↓ 213␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Stei 74]␈↓ αxSteiger, R., Actor machine architecture, M.S. Thesis, M.I.T., 1974 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Sto 75]␈↓ αxStoyan,␈α∃H.,␈α∃Comparison␈α∃of␈α∃two␈α∃LISP␈α∃compilers: Stanford␈α∃versus␈α∀DOS/ES-LISP,
␈↓ ↓H␈↓␈↓ βλ␈↓αElektronische Informationsverarbeitung und Kybernetik 11␈↓, 371-375 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Str 67]␈↓ αxStrachey,␈α∞C.,␈α∞Fundamental␈α∞concepts␈α∞in␈α∞programming␈α∞languages,␈α∂NATO␈α∞Conference,
␈↓ ↓H␈↓␈↓ βλCopenhagen, 1967 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Str 74]␈↓ αxFoy,␈α↔N.,␈α↔The␈α↔Words␈α↔Games␈α↔of␈α⊗the␈α↔Night␈α↔Bird:␈α↔Interview␈α↔with␈α↔C.␈α⊗Strachey,
␈↓ ↓H␈↓␈↓ βλ␈↓αComputing Europe␈↓, Aug 15, 1974, pp10-11 ␈↓↓[␈↓␈↓↓ 86␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Sus 75]␈↓ αxSussman,␈α∂G.␈α∂&␈α∂Steel,␈α∂G.,␈α∂SCHEME:␈α∂an␈α∂interpreter␈α∂for␈α∂extended␈α∂Lambda␈α∞Calculus,
␈↓ ↓H␈↓␈↓ βλM.I.T. A.I. Memo 349, Dec. 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Sus 76]␈↓ αxSussman, G. & Steel, G., SCHEME Flash  1, M.I.T. A.I.Lab., Jan. 1976 ␈↓↓[␈↓␈↓↓ 167␈↓␈↓↓ 192␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓7.␈↓ 	
BIBLIOGRPAHY     257␈↓


␈↓ ↓H␈↓[Tei 72]␈↓ αxTeitelman,␈αW.,␈αAutomated␈αProgrammering - The␈αprogrammer's␈αassistant,␈αProc.␈αof␈αthe
␈↓ ↓H␈↓␈↓ βλFall Joint Computer Confr., Dec. 1972 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ten 76]␈↓ αxTennent,R.,␈α∞The␈α∞denotational␈α∂semantics␈α∞of␈α∞programming␈α∂languages,␈α∞␈↓αComm. ACM 19␈↓,
␈↓ ↓H␈↓␈↓ βλ␈↓↓8␈↓, (Aug. 1967), 437-453 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Ter 75]␈↓ αxTerashima,␈α→M.,␈α~Algorithms␈α→used␈α→in␈α~an␈α→implementation␈α→of␈α~HLISP,␈α→Tr 75-03,
␈↓ ↓H␈↓␈↓ βλInformation Sciences Lab. Univ. of Tokyo, Japan, Jan. 1975 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Vui 73]␈↓ αx169 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wad 71]␈↓ αxWadsworth,␈α≤C.,␈α≠Semantics␈α≤and␈α≠Pragmatics␈α≤of␈α≠the␈α≤Lambda-calculus,␈α≠Oxford,
␈↓ ↓H␈↓␈↓ βλ1971 ␈↓↓[␈↓␈↓↓ 169␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wad 74]␈↓ αxWadsworth, C., continuations paper ␈↓↓[␈↓␈↓↓ 155␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wad xx]␈↓ αxWadsworth,␈α∪C.,␈α∪The␈α∪relation␈α∩between␈α∪Lambda-expressions␈α∪and␈α∪their␈α∩denotations,
␈↓ ↓H␈↓␈↓ βλunpublished paper, Systems and Info. Science Dept, Syracuse Univ., 19xx ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[War 74]␈↓ αxWard,␈α∂S.,␈α∂Functional␈α⊂domains␈α∂of␈α∂applicative␈α∂languages,␈α⊂Ph.d. thesis,␈α∂MAC TR-136,
␈↓ ↓H␈↓␈↓ βλM.I.T., Cambridge, Sep. 1974 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Weg 68]␈↓ αxWegner,␈α
P.,Programming␈α
languages,␈α
information␈α
structures␈α
and␈αmachine␈α
orgainzation,
␈↓ ↓H␈↓␈↓ βλMcGraw-Hill, 1968 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Weg 69]␈↓ αxWegner, P., The Vienna Definition Language, Computing Surveys ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Weg 70]␈↓ αxWegner,␈αP.,␈αThree␈αComputer␈αCultures-computer␈αtechnology,␈αcomputer␈αmathematics␈α&
␈↓ ↓H␈↓␈↓ βλcomputer science,Advances in Computers, ␈↓↓10␈↓, 1970 ␈↓↓[␈↓␈↓↓ 1␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Weg 71]␈↓ αxWegner,␈α↔P.,␈α↔Data␈α↔Structure␈α⊗Models␈α↔for␈α↔Programming␈α↔Languages,␈α↔in␈α⊗[DISPL],
␈↓ ↓H␈↓␈↓ βλ1-54 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wegb 70]␈↓ αxWegbreit,␈α∩B.,␈α∪Studies␈α∩in␈α∪Extensible␈α∩Programming␈α∪LanguagesPh.D.thesis.␈α∩Harvard
␈↓ ↓H␈↓␈↓ βλUniversity, Cambridge, Mass.,1970 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wegb 74]␈↓ αxWegbreit,␈αB.,␈α
The␈αTreatment␈α
of␈αData␈α
Types␈αin␈αEL1,␈α
␈↓αComm.␈αACM␈α
17␈↓,␈α␈↓↓5␈↓,␈α
(May␈α1974),
␈↓ ↓H␈↓␈↓ βλ251-264. ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wegb 75]␈↓ αxWegbreit,␈α∀B.,␈α∀Retrieval␈α∀from␈α∀context␈α∪trees,␈α∀Information␈α∀Processing␈α∀Letters,␈α∀3,␈α∪␈↓↓4␈↓,
␈↓ ↓H␈↓␈↓ βλ119-120, (March 1975) ␈↓↓[␈↓␈↓↓ 132␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wei 63]␈↓ αxWeismann, C.,LISP 1.5 primer, Dickenson Press, 1967 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wei 68]␈↓ αxWeizenbaum,␈α∪J.,␈α∪The␈α∀FUNARG␈α∪Problem␈α∪Explained,␈α∀unpublished␈α∪memorandum,
␈↓ ↓H␈↓␈↓ βλM.I.T., Cambridge, Mass., 1968 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓
␈↓ ↓H␈↓␈↓↓258  BIBLIOGRAPHY␈↓ C7.␈↓


␈↓ ↓H␈↓[Win 75]␈↓ αxWinograd,␈α
T.,␈α∞Breaking␈α
the␈α∞complexity␈α
barrier (again),␈↓αACM␈α∞SIGPLAN␈α
Notes␈α∞10␈↓,␈α
␈↓↓1␈↓,
␈↓ ↓H␈↓␈↓ βλ(Jan. 1975), 13-30 ␈↓↓[␈↓␈↓↓ norefs␈↓␈↓↓ ]␈↓

␈↓ ↓H␈↓[Wis 75]␈↓ αxWise,␈α∀D.,␈α∀␈↓αet.al␈↓,␈α∪Boolean-valued␈α∀loops,␈α∀Tr.␈α∪No.␈α∀21,␈α∀Indiana␈α∀University␈α∪Computer
␈↓ ↓H␈↓␈↓ βλScience Dept, Jan. 1975 ␈↓↓[␈↓␈↓↓ 145␈↓
␈↓ ↓H␈↓␈↓↓␈↓ 
∀INDEX     259␈↓


␈↓ ↓H␈↓␈↓αλ␈↓-notation   95                           ␈↓ εh␈↓αread_head   208
␈↓ ↓H␈↓␈↓αand␈↓   134                                 ␈↓ εh␈↓αread_tail   208
␈↓ ↓H␈↓␈↓αappend␈↓   49                               ␈↓ εh␈↓αread   208
␈↓ ↓H␈↓␈↓αapply␈↓   101                               ␈↓ εh␈↓αread␈↓ macro   215
␈↓ ↓H␈↓␈↓αatom␈↓   22                                 ␈↓ εh␈↓␈↓αremprop␈↓   198
␈↓ ↓H␈↓␈↓αBLANK␈↓   207                               ␈↓ εh␈↓␈↓αrest␈↓   32
␈↓ ↓H␈↓␈↓αcar␈↓   17                                  ␈↓ εh␈↓␈↓αreverse␈↓   49
␈↓ ↓H␈↓␈↓αcar-cdr-␈↓chains   18                       ␈↓ εh␈↓␈↓αRPAR␈↓   207
␈↓ ↓H␈↓␈↓αcatch␈↓   148                               ␈↓ εh␈↓␈↓αseq␈↓   32
␈↓ ↓H␈↓␈↓αcdr␈↓   17                                  ␈↓ εh␈↓␈↓αtgmoaf␈↓   81
␈↓ ↓H␈↓␈↓αconcat␈↓   32                               ␈↓ εh␈↓␈↓αtgmoaf␈↓   243
␈↓ ↓H␈↓␈↓αCOND␈↓   92, 99                             ␈↓ εh␈↓␈↓αtgmoafr␈↓   81
␈↓ ↓H␈↓␈↓αcons␈↓   16, 216                            ␈↓ εh␈↓␈↓αthrow␈↓   148
␈↓ ↓H␈↓␈↓αeq␈↓   22                                   ␈↓ εh␈↓a-lists   93
␈↓ ↓H␈↓␈↓αequal␈↓   26                                ␈↓ εh␈↓access chain   113
␈↓ ↓H␈↓␈↓αerr␈↓   147                                 ␈↓ εh␈↓access link   113
␈↓ ↓H␈↓␈↓αerrset␈↓   147                              ␈↓ εh␈↓activation environment   128
␈↓ ↓H␈↓␈↓αeval␈↓   99, 178, 236                       ␈↓ εh␈↓actual parameters   19
␈↓ ↓H␈↓␈↓αevcond␈↓   99                               ␈↓ εh␈↓AMBIT/G   190
␈↓ ↓H␈↓␈↓αFUNARG␈↓   128                              ␈↓ εh␈↓assignment statement   137
␈↓ ↓H␈↓␈↓αfunction␈↓   123                            ␈↓ εh␈↓association lists   93
␈↓ ↓H␈↓␈↓αget␈↓   197                                 ␈↓ εh␈↓atom header   202
␈↓ ↓H␈↓␈↓αgetl␈↓   197                                ␈↓ εh␈↓atom space   182
␈↓ ↓H␈↓␈↓αgo␈↓   139                                  ␈↓ εh␈↓auxiliary function   48
␈↓ ↓H␈↓␈↓αisseq␈↓   31                                ␈↓ εh␈↓bind   89, 101
␈↓ ↓H␈↓␈↓αlabel␈↓   118                               ␈↓ εh␈↓binding environment   128
␈↓ ↓H␈↓␈↓αlist   38                                 ␈↓ εh␈↓binding strategy   131
␈↓ ↓H␈↓αLPAR   207                                ␈↓ εh␈↓bound variable   111, 112
␈↓ ↓H␈↓αmaplist   130                             ␈↓ εh␈↓box-notation   13
␈↓ ↓H␈↓αmkent   94                                ␈↓ εh␈↓bucket   211
␈↓ ↓H␈↓αNIL   36, 206                             ␈↓ εh␈↓call-by-name   88, 168
␈↓ ↓H␈↓αnull   31                                 ␈↓ εh␈↓call-by-need   169
␈↓ ↓H␈↓αOBLIST   211                              ␈↓ εh␈↓call-by-value   88
␈↓ ↓H␈↓αor   134                                  ␈↓ εh␈↓case  statement   143
␈↓ ↓H␈↓αPERIOD   207                              ␈↓ εh␈↓case statement   136
␈↓ ↓H␈↓αPNAME   203                               ␈↓ εh␈↓closure   123, 142
␈↓ ↓H␈↓αprin0   209                               ␈↓ εh␈↓coercion   179
␈↓ ↓H␈↓αprin1   207                               ␈↓ εh␈↓computed function   175
␈↓ ↓H␈↓αprint   209                               ␈↓ εh␈↓conditional expression   21, 99
␈↓ ↓H␈↓αprog   136                                ␈↓ εh␈↓CONNIVER   236
␈↓ ↓H␈↓αprog variables   137                      ␈↓ εh␈↓constructor   16
␈↓ ↓H␈↓αputprop   197                             ␈↓ εh␈↓continuation   155
␈↓ ↓H␈↓αQUOTE   91                                ␈↓ εh␈↓control environment   124
␈↓ ↓H␈↓αratom   207, 211, 213                     ␈↓ εh␈↓control structures   41
␈↓ ↓H␈↓␈↓↓260  INDEX␈↓ X␈↓


␈↓ ↓H␈↓data structures   2                       ␈↓ εh␈↓lists   34
␈↓ ↓H␈↓deep binding   134                        ␈↓ εh␈↓literal atoms   9
␈↓ ↓H␈↓definition by recursion   44              ␈↓ εh␈↓local binding   111
␈↓ ↓H␈↓differentiation   56                      ␈↓ εh␈↓local symbol table   111
␈↓ ↓H␈↓discriminator   22                        ␈↓ εh␈↓local variable   137
␈↓ ↓H␈↓dotted-pair   10                          ␈↓ εh␈↓M-expressions   178
␈↓ ↓H␈↓doubly-linked list structure   184        ␈↓ εh␈↓mapping functions   130
␈↓ ↓H␈↓dynamic binding   112                     ␈↓ εh␈↓marking phase   218
␈↓ ↓H␈↓evaluation   86                           ␈↓ εh␈↓match-variable   11
␈↓ ↓H␈↓examples of ␈↓αeval␈↓   102                    ␈↓ εh␈↓meta-language   178
␈↓ ↓H␈↓expr   172                                ␈↓ εh␈↓MICRO-PLANNER   236
␈↓ ↓H␈↓fexpr   172                               ␈↓ εh␈↓monotonic functions   26
␈↓ ↓H␈↓Fibonacci sequence   47                   ␈↓ εh␈↓MUDDLE   236
␈↓ ↓H␈↓form   21, 96                             ␈↓ εh␈↓name stack   226
␈↓ ↓H␈↓formal parameters   18                    ␈↓ εh␈↓non-local   111
␈↓ ↓H␈↓forms   17                                ␈↓ εh␈↓non-strict   24
␈↓ ↓H␈↓free space list   216                     ␈↓ εh␈↓object list   211
␈↓ ↓H␈↓free variable   111                       ␈↓ εh␈↓p-list   202
␈↓ ↓H␈↓Full Word Space   204                     ␈↓ εh␈↓parser   207
␈↓ ↓H␈↓funarg   123                              ␈↓ εh␈↓partial function   15
␈↓ ↓H␈↓function   96                             ␈↓ εh␈↓partial functions   17
␈↓ ↓H␈↓function application   18                 ␈↓ εh␈↓pointer   182
␈↓ ↓H␈↓functional argument   120                 ␈↓ εh␈↓pointer space   182
␈↓ ↓H␈↓functional composition   17               ␈↓ εh␈↓polymorphic functions   32
␈↓ ↓H␈↓functional value   125                    ␈↓ εh␈↓precedence   239
␈↓ ↓H␈↓FWS   204                                 ␈↓ εh␈↓predicates   21
␈↓ ↓H␈↓garbage collection   217                  ␈↓ εh␈↓prefix notation   57
␈↓ ↓H␈↓garbage collector   217                   ␈↓ εh␈↓print-name   203
␈↓ ↓H␈↓generalized control structures   146      ␈↓ εh␈↓property-list   193
␈↓ ↓H␈↓global variable   111, 112                ␈↓ εh␈↓recognizer   22, 31
␈↓ ↓H␈↓hash consing   221                        ␈↓ εh␈↓reference counter   221, 233
␈↓ ↓H␈↓hashing   211                             ␈↓ εh␈↓S-expressions   9
␈↓ ↓H␈↓hashing function   211                    ␈↓ εh␈↓S-exprs   9
␈↓ ↓H␈↓inductive definition   7                  ␈↓ εh␈↓scanner   207
␈↓ ↓H␈↓internal lambdas   98                     ␈↓ εh␈↓SDIO   244
␈↓ ↓H␈↓lambda variables   96                     ␈↓ εh␈↓selector   17
␈↓ ↓H␈↓length   136                              ␈↓ εh␈↓self-applicative functions   131
␈↓ ↓H␈↓linear search   94                        ␈↓ εh␈↓shallow binding   134
␈↓ ↓H␈↓linked allocation   225                   ␈↓ εh␈↓side-effects   137
␈↓ ↓H␈↓linked list structure   183               ␈↓ εh␈↓singly linked   183
␈↓ ↓H␈↓LISP machine   222                        ␈↓ εh␈↓Special Form   134
␈↓ ↓H␈↓list   36                                 ␈↓ εh␈↓special forms   99
␈↓ ↓H␈↓list terminator   36                      ␈↓ εh␈↓stack   225
␈↓ ↓H␈↓list-notation   36                        ␈↓ εh␈↓statement   137
␈↓ ↓H␈↓␈↓↓␈↓ 
∀INDEX     261␈↓


␈↓ ↓H␈↓static binding   131
␈↓ ↓H␈↓storage reclaimer   217
␈↓ ↓H␈↓storage structures   2
␈↓ ↓H␈↓stratification   247
␈↓ ↓H␈↓strict functions   16
␈↓ ↓H␈↓sweep phase   218
␈↓ ↓H␈↓symbol tables   93
␈↓ ↓H␈↓Symbolic expressions   9
␈↓ ↓H␈↓syntax-directed   239, 248
␈↓ ↓H␈↓table-driven   141
␈↓ ↓H␈↓termination conditions   46
␈↓ ↓H␈↓total function   15
␈↓ ↓H␈↓type fault   26
␈↓ ↓H␈↓unbound variable   112
␈↓ ↓H␈↓value stack   226
␈↓ ↓H␈↓variables   110